DK

by Sam Fereday

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser-ce/2.11.0/phaser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bin/easystar-0.4.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.0/redux.min.js"></script>
<div id="phaser-example"></div>

JavaScript 1.7

/*
https://jsfiddle.net/juwalbose/pu0gt7nc/
https://github.com/prettymuchbryce/easystarjs
DK clone prototype in JS. 3D? Well that'd need to be done in Unity in future if that's the case.
Start with Imps.
What does an Imp do?
- during idle phase is runs around doing bugger all, unless a task appears that's not been taken such as claiming land, reinforcing a wall, picking stuff up and dragging it somewhere (generic, the thing will have a location, imps don't need to care about that part)
- if being attacked they'll raise alarms, run back to the core, and if the core is under attack then they'll attack as a last resort

For part one, an Imp must:
- Run to a location that's highlighted on the map, this selected location will be dirt
- When the dirt has been cleared, the imp will do nothing
*/
// Raw tiles to set up state from, this is the initial state of the map that will never change in future.
const Tiles = [
  [1, 1, 1, 1, 1, 1],
  [1, 3, 0, 0, 2, 1],
  [1, 1, 1, 1, 1, 1]
]

const TILE_TYPES = {
  DIRT: 0,
  IMPENETRABLE: 1,
  DIGGABLE: 2,
  CLAIMED: 3
}

// Inflates the array again (not ideal, but pathfinding expects it...), could use a reducer here actually.
const inflate = (arr) => {
  const rows = arr[arr.length - 1].y;
  const cols = arr[arr.length - 1].x;

  let memo = [];

  for (let i = 0; i <= rows; i++) {
    memo.push([]);
    for (let j = 0; j <= cols; j++) {
      const v = arr.find(item => item.x === j && item.y === i).value;
      memo[i].push(v);
    }
  }

  return memo;
}

// The rest is squishy
const updateTile = ({
  x,
  y,
  value
}) => ({
  type: 'UPDATE_TILE',
  x,
  y,
  value
});

const addTile = ({
  x,
  y,
  value
}) => ({
  type: 'ADD_TILE',
  x,
  y,
  value
});

const tileReducer = (state = [], action) => {
  if (typeof state === 'undefined') {
    return [];
  }

  switch (action.type) {
    case 'UPDATE_TILE':
      return state.map(tile => {
        return tile.x === action.x && tile.y === action.y ? {
            ...tile,
     ...