all repos — uStrat @ b91fdb49ec7308d4d8b43e7014dc997a499d848b

simple turn-based strategy game inspired by uCity, Super Robot Wars, C&C, Fire Emblem

Movement.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
window.movement = {};
movement.chain = new Array();
movement.unit = {};
movement.pool = 0;
movement.animCounter = 0;
movement.gfx = {};
movement.gfx.overlay = new Image();
movement.gfx.overlay.src = "assets/ui/movementOverlay.png";

function Coords(x,y)
{
  this.x = x;
  this.y = y;
}

movement.init = function(u)
{
  this.unit = u;
  this.pool = u.agi;
  this.chain.push(new Coords(u.x, u.y));
}

movement.cancel = function()
{
  this.unit = {};
  this.pool = 0;
  this.chain = new Array();
  gameState.flow = "freeLook";
}

movement.listen = function()
{
  var z = {};
  var chainEnd = this.chain[this.chain.length - 1];

  if (controller.z)
  {
    while (this.chain.length > 1)
    {
      this.chain.pop();
      this.pool = this.unit.agi;
      mapCursor.x = this.chain[0].x;
      mapCursor.y = this.chain[0].y;
    }
  }

  if (controller.x)
  {
    this.cancel();
    return;
  }

  if (controller.y)
  {
    this.confirm();
    return;
  }

  if (controller.up)
  {
    z = new Coords(chainEnd.x, chainEnd.y - 1);
  }

  if (controller.left)
  {
    z = new Coords(chainEnd.x - 1, chainEnd.y);
  }

  if (controller.down)
  {
    z = new Coords(chainEnd.x, chainEnd.y + 1);
  }

  if (controller.right)
  {
    z = new Coords(chainEnd.x + 1, chainEnd.y);
  }

  if (!isEmptyObject(z))
  {
    if (map.data[z.x][z.y].agiMod <= this.pool && z.x >= 0 && z.x < 64 && z.y >= 0 && z.y < 64 && isEmptyObject(map.data[z.x][z.y].unit))
    {
      if (this.unit.name != "p1 Battle Angel")
      {
        this.pool -= map.data[z.x][z.y].agiMod + 1;
      }
      else { this.pool -= 1; }
      this.chain.push(z);
      mapCursor.x = z.x;
      mapCursor.y = z.y;
    }
  }
}

movement.showChain = function()
{
  var c, i, j;
  screen.font = "8px console";
  screen.textBaseline = "top";
  screen.fillStyle = "white";
  for (c = 0; c < this.chain.length; c++)
  {
    screen.drawImage(this.gfx.overlay, 16*(this.chain[c].x - camera.x), 16*(this.chain[c].y - camera.y));
  }
}

movement.confirm = function()
{
  gameState.flow = "moveAnim";
}

movement.go = function()
{
  this.animCounter++;
  if (this.animCounter == 10)
  {
    this.animCounter = 0;
    if (this.chain.length > 1)
    {
      this.chain.shift();
    }
    else
    {
      this.unit.hasMoved = true;
      this.unit = {};
      this.chain = new Array();
      this.pool = 0;
      gameState.flow = "freeLook";
      return;
    }
    moveToTile(this.unit, this.chain[0]);
  }
}

function moveToTile(unit, tile)
{
  var oX, oY;
  oX = unit.x;
  oY = unit.y;
  map.data[tile.x][tile.y].unit = unit;
  unit.x = tile.x;
  unit.y = tile.y;
  map.data[oX][oY].unit = {}
}