Новое в ОО, я сделать игру, и я это как мой initialState
:Javascript DRY Pattern - функция
let initialState = {
grid : null,
player : {
coordinates : null,
health : 100 ,
weapon : "Stick",
expLevel : 1
},
enemies : [],
weapons : [],
items : []
}
, когда DOM загружен, я могу инициализировать weapons
и items
corretly. Однако оба этих свойств разделяют точную функциональность, кроме литерал объекта, который инициализируется:
initialState.weapons = placeWeapons(3);
initialState.items = placeItems(2);
где placeWeapons
function placeWeapons(numberofWeapons){
let weapons = [];
let availableSpots = [];
let placementWeapons = []; //list of coordinates that will place weapons
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach((row, rowIndex) => (
row.forEach((cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the weapons. When placed, it will update initialState.occupiedCoordinates
while(placementWeapons.length < numberofWeapons){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementWeapons.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementWeapons.forEach(coord => {
let weapon = {
name : "Weapon Name",
coords : coord,
damage : 3
}
weapons.push(weapon)
})
return weapons;
}
placeItems
function placeItems(numberofItems){
let items = [];
let availableSpots = [];
let placementItems = []; //list of coordinates that will place items
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach((row, rowIndex) => (
row.forEach((cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the items. When placed, it will update initialState.occupiedCoordinates
while(placementItems.length < numberofItems){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementItems.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementItems.forEach(coord => {
let item = {
name : "Item Name",
coords : coord,
health : 3
}
items.push(item)
})
return items;
}
Как я могу Высушите этот шаблон?
Сделать это проще, написав функцию, которая возвращает массив возможных мест размещения. – Bergi