all repos — uStrat @ e130b5b6df964e5cf1ddf75bd25a980ea3d1f478

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

Attack.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
window.attack = {};
attack.pattern = {};
attack.rotations = [ 0 ];
attack.rotation = 0;
attack.src = {};

attack.gfx = {};
attack.gfx.overlay = new Image()
attack.gfx.overlay.src = "assets/ui/attackOverlay.png";
attack.gfx.explosion = new Image()
attack.gfx.explosion.src = "assets/fx/explosion.png";

attack.animCounter = 0;
attack.frame = 0;

attack.init = function()
{
  gameState.flow = "attackSelect";
  attack.src = map.data[mapCursor.x][mapCursor.y].unit;
  attack.pattern = attack.src.equipment.pattern;
}

attack.cancel = function()
{
  gameState.flow = "freeLook";
}

attack.confirm = function()
{
  gameState.flow = "attackAnim";
}

attack.go = function()
{
  var pattern = rotatePattern(this.pattern, this.rotations[this.rotation]);
  var origin = {};
  var i, j;
  origin.x = mapCursor.x - 2;
  origin.y = mapCursor.y - 2;
  this.animCounter++;
  for (i = 0; i < 5; i++)
  {
    for (j = 0; j < 5; j++)
    {
      if (pattern[i][j] != 0)
      {
        screen.drawImage(this.gfx.explosion, this.frame*16, 0, 16, 16, 16*(origin.x + i - camera.x), 16*(origin.y + j - camera.y), 16, 16);
        if (this.animCounter == 3 && this.frame == 2)
        {
    damageStruct(origin.x + i, origin.y + j);
    damageUnit(origin.x + i, origin.y + j);
    map.degradeCell(map.data[origin.x + i][origin.y + j]);
        }
      }
    }
  }
  if (this.animCounter == 3)
  {
    this.frame++;
    this.animCounter = 0;
  }
  if (this.frame == 3)
  {
    this.frame = 0;
    this.animCounter = 0;
    this.src.hasAttacked = true;
    if (gameState.flow != "youLose" && gameState.flow != "youWin")
    {
      gameState.flow = "attackEnd";
    }
  }
}

attack.listen = function()
{
  var backupCursor =
  {
    x: mapCursor.x,
    y: mapCursor.y
  };

  if (controller.up || controller.down || controller.left || controller.right)
  {
    mapCursor.manage();
    camera.manage();
  }
  if (dist(mapCursor, this.src) > this.src.equipment.range)
  {
    mapCursor.x = backupCursor.x;
    mapCursor.y = backupCursor.y;
  }
  this.manageRotations();
  if (controller.w)
  {
    this.rotation++;
    if (this.rotation == this.rotations.length)
    {
      this.rotation = 0;
    }
  }
  if (controller.x)
  {
    attack.cancel();
  }
  if (controller.y)
  {
    if (mapCursor.x != this.src.x || mapCursor.y != this.src.y)
     attack.confirm();
  }
}

attack.manageRotations = function()
{
  if (mapCursor.x < this.src.x)
  {
    if (xDist(mapCursor, this.src) > yDist(mapCursor, this.src))
    {
      this.rotations = [ 1 ];
    }
    else if (xDist(mapCursor, this.src) < yDist(mapCursor, this.src))
    {
      if (mapCursor.y < this.src.y)
      {
        this.rotations = [ 0 ];
      }
      else { this.rotations = [ 2 ]; }
    }
    else
    {
      if (mapCursor.y < this.src.y)
      {
        this.rotations = [ 0, 1 ];
      }
      else { this.rotations = [ 2, 1 ]; }
    }
  }
  if (mapCursor.x > this.src.x)
  {
    if (xDist(mapCursor, this.src) > yDist(mapCursor, this.src))
    {
      this.rotations = [ 3 ];
    }
    else if (xDist(mapCursor, this.src) < yDist(mapCursor, this.src))
    {
      if (mapCursor.y < this.src.y)
      {
        this.rotations = [ 0 ];
      }
      else { this.rotations = [ 2 ]; }
    }
    else
    {
      if (mapCursor.y < this.src.y)
      {
        this.rotations = [ 0, 3 ];
      }
      else { this.rotations = [ 3, 2 ]; }
    }
  }
  if (mapCursor.y < this.src.y)
  {
    if (xDist(mapCursor, this.src) > yDist(mapCursor, this.src))
    {
      if (mapCursor.x > this.src.x)
      {
        this.rotations = [ 0 ];
      }
      else { this.rotations = [ 1 ]; }
    }
    else if (xDist(mapCursor, this.src) < yDist(mapCursor, this.src))
    {
      this.rotations = [ 0 ];
    }
    else
    {
      if (mapCursor.x < this.src.x)
      {
        this.rotations = [ 0, 1 ];
      }
      else { this.rotations = [ 0, 3 ]; }
    }
  }
  if (mapCursor.y > this.src.y)
  {
    if (xDist(mapCursor, this.src) > yDist(mapCursor, this.src))
    {
      if (mapCursor.x > this.src.x)
      {
        this.rotations = [ 3 ];
      }
      else { this.rotations = [ 1 ]; }
    }
    else if (xDist(mapCursor, this.src) < yDist(mapCursor, this.src))
    {
      this.rotations = [ 2 ];
    }
    else
    {
      if ( mapCursor.x > this.src.x)
      {
        this.rotations = [ 2, 3 ];
      }
      else { this.rotations = [ 2, 1 ]; }
    }
  }
  if (this.rotations.length == 1)
  {
    this.rotation = 0;
  }
}

attack.drawPattern = function()
{
  var pattern = rotatePattern(this.pattern, this.rotations[this.rotation]);
  var i, j;
  var origin = {}
  origin.x = mapCursor.x - 2,
  origin.y = mapCursor.y - 2;
  console.log(this.rotations);
  for (i = 0; i < 5; i++)
  {
    for (j = 0; j < 5; j++)
    {
      if (pattern[i][j] != 0)
      {
        if (mapCursor.x != this.src.x || mapCursor.y != this.src.y)
        {
          screen.drawImage(this.gfx.overlay, 16*(origin.x + i - camera.x), 16*(origin.y + j - camera.y));
        }
      }
    }
  }
}