2016-11-02 9 views
0

新しい、私はゲームを作っていると私は私のinitialStateとしてこれを持っている:Javascriptの乾燥パターン - OOに機能

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

DOMがロードされたとき、私はcorretly weaponsitemsを初期化することができています。私は

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); 

あなたが置かれているものがランダムにしたい場合は、単に渡すことができます代わりに、オブジェクトの機能:

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

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

    /* 
    * bla bla.. 
    */ 

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

だからあなたのような何かをしたいです

+0

驚くばかり!それはトリックをした:) – Alejandro

関連する問題