all repos — uStrat @ 2702b9c7d19824a4be38b47709e1f6213b4292c7

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

Engine.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
window.gameState = {};
window.controller = {};

gameState.paused = false;
gameState.playing = false;
gameState.over = false;
gameState.ruin = 0;
gameState.phase = "p1";
gameState.flow = "freeLook";

gameState.ticker = new Date();
gameState.frame = new Date();

controller.up = false;
controller.down = false;
controller.right = false;
controller.left = false;
controller.start = false;
controller.q = false;
controller.w = false;
controller.x = false;
controller.y = false;
controller.z = false;

function isEmptyObject(obj)
{
  var name;
  for (name in obj)
  {
    if (obj.hasOwnProperty(name))
    {
      return false;
    }
  }
  return true;
}

function dist(a, b)
{
  return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}

function xDist(a, b)
{
  return Math.abs(a.x - b.x);
}

function yDist(a, b)
{
  return Math.abs(a.y - b.y);
}

function endPhase()
{
  var i, totalE, tm;
  totalE = 0;
  switch(gameState.phase)
  {
    case "p1":
      tm = teams.p1;
      gameState.phase = "cpu";
      resetFlow();
      break;
    case "cpu":
      tm = teams.cpu;
      gameState.phase = "p1";
      resetFlow();
      break;
  }
  for (i = 0; i < tm.structs.length; i++)
  {
    totalE += tm.structs[i].energy;
    tm.energy += tm.structs[i].energy*tm.structs[i].rate;
  }
  if (tm.energy >= totalE) tm.energy = totalE;
  for (i = 0; i < tm.units.length; i++)
  {
    tm.units[i].hasMoved = false;
    tm.units[i].hasAttacked = false;
  }
}

function freeLook()
{
  mapCursor.manage();
  camera.manage();
  map.draw();
  mapCursor.draw();
  hud.draw();
}

function navigateMenu()
{
  menuSys.input();
  map.draw();
  mapCursor.draw();
  hud.draw();
  menuSys.draw();
}

function moveSelect()
{
  movement.listen();
  camera.manage();
  map.draw();
  movement.showChain();
  mapCursor.draw();
  hud.draw();
}

function moveAnim()
{
  movement.go();
  map.draw();
}

function miniMap()
{
  minimap.listen();
  map.draw();
  minimap.draw();
}

function attackSelect()
{
  attack.listen();
  map.draw();
  attack.drawPattern();
  mapCursor.draw();
}

function attackAnim()
{
  map.draw();
  attack.go();
}

function attackEnd()
{
  switch (gameState.phase)
  {
    case "cpu":
      gameState.flow = "cpuManageUnits";
      break;
    case "p1":
      gameState.flow = "freeLook";
      break;
  }
}

function loop()
{
  thisFrame = new Date();
  if (thisFrame - gameState.frame < 33.34)
  {
    requestAnimationFrame(loop);
  }
  else
  {
    gameState.frame = thisFrame;
    switch (gameState.flow)
    {
      case "freeLook":
        freeLook();
        break;
      case "miniMap":
        miniMap();
        break;
      case "mainMenu":
      case "buildMenu":
      case "fabricateMenu":
      case "equipmentMenu":
        navigateMenu();
        break;
      case "moveUnit":
        moveSelect();
        break;
      case "moveAnim":
        moveAnim();
        break;
      case "attackSelect":
        attackSelect();
        break;
      case "attackAnim":
        attackAnim();
        break;
      case "attackEnd":
        attackEnd();
        break;
    }

    requestAnimationFrame(loop);
  }
}