2016-11-02 56 views
0

新建OO,我想提出一個遊戲,我有這個作爲我initialState的Javascript乾花樣 - 功能

let initialState = { 
    grid : null, 
    player : { 
    coordinates : null, 
    health : 100 , 
    weapon : "Stick", 
    expLevel : 1 
    }, 
    enemies : [], 
    weapons : [], 
    items : [] 
} 

加載DOM的時候,我能夠初始化weaponsitems 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; 
} 

我怎樣才能幹這種模式?

+0

通過編寫一個返回可能的展示位置數組的函數來簡化操作。 – Bergi

回答

2

我看到的唯一顯着差異就是放置的東西。所以,做合乎邏輯的做法是提取並把它作爲一個參數:

// WARNING: Untested code: 
function placeThings(thing, numberofThings){ 
    let things = []; 
    let availableSpots = []; 
    let placementItems = []; //list of coordinates that will place things 
    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 < numberofThings){ 
    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 => { 
    things.push(Object.create(thing)) 
    }) 
    return things; 
} 

現在你可以這樣做:

// Weapons: 
placeThings({ 
    name : "Weapon Name", 
    coords : coord, 
    damage : 3 
},50); 

// Items: 
placeThings({ 
    name : "Item Name", 
    coords : coord, 
    health : 3 
},100); 

如果你想的東西放在隨機你可以通過一個函數,而不是一個對象:

// WARNING: Untested code: 
function placeThings(thingGenerator, numberofThings){ 
    let things = []; 

    /* 
    * bla bla.. 
    */ 

    placementItems.forEach(coord => { 
    things.push(thingGenerator()) 
    }) 
    return things; 
} 

所以,你會做這樣的事情:

placeThings(makeWeapon, 50); 
placeThings(makeItem, 100); 
+0

太棒了!這做了一個竅門:) – Alejandro