all repos — uStrat @ 52186a25550d945fd5ee6e2ac79804b564d82921

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
145
146
147
148
149
150
151
152
153
154
155
156
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";
movement.ticker = new Date();
movement.lastMove = new Date();


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];
  this.ticker = new Date();
  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 (this.ticker - this.lastMove >= 200)
  {

  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);
  }
    this.lastMove = this.ticker;
  }
  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()
{
  movement.animCounter++;
  if (movement.animCounter == 10)
  {
    movement.animCounter = 0;
    if (movement.chain.length > 1)
    {
      movement.chain.shift();
    }
    else
    {
      movement.unit.hasMoved = true;
      movement.unit = {};
      movement.chain = new Array();
      movement.pool = 0;
      if (gameState.phase == "p1")
      {
        gameState.flow = "freeLook";
      }
      else {
        AI.focus++;
        gameState.flow = "cpuMoveDone"; }
      return;
    }
    moveToTile(movement.unit, movement.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 = {}
}