-
Notifications
You must be signed in to change notification settings - Fork 0
/
mission.d
473 lines (425 loc) · 18 KB
/
mission.d
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import std.stdio;
import std.file;
import std.path : buildNormalizedPath;
import std.string : toStringz;
import std.conv;
import std.json;
import std.algorithm.searching;
import std.datetime.stopwatch;
import raylib;
import map;
import tile;
import unit;
import vector_math;
import vtile;
const int TILESIZE = 64;
class Mission : Map
{
Texture2D[] sprites;
Texture2D gridMarker;
int[string] spriteIndex;
Unit* selectedUnit;
Rectangle[][] gridRects;
GridTile[][] squareGrid;
Vector2 offset;
Rectangle mapView;
Vector2i mapSizePx;
GridTile[] startingPoints;
this() {
JSONValue missionData = parseJSON(readText("../maps/Test_battlefield.json"));
this(missionData);
}
this(string missionPath) {
JSONValue missionData = parseJSON(readText(missionPath));
this(missionData);
}
this(JSONValue mapData) {
this.offset = Vector2(0.0f, 0.0f);
import std.algorithm;
import std.conv;
super(mapData["map_name"].get!string);
super.loadFactionsFromJSON(mapData);
this.grid.length = mapData.object["tiles"].array.length;
this.squareGrid.length = mapData.object["tiles"].array.length;
writeln("Starting to unload tile data");
foreach (int x, tileRow; mapData.object["tiles"].array) {
foreach (int y, tileData; tileRow.arrayNoRef) {
string tileName = "";
if ("name" in tileData) tileName = tileData["name"].get!string;
bool allowStand = tileData["canWalk"].get!bool;
bool allowFly = true;// tile["canFly"].get!bool;
int stickiness = tileData["stickiness"].get!int;
string spriteName = tileData["tile_sprite"].get!string;
ushort spriteID;
spriteID = loadNumberTexture(spriteName, spriteIndex, this.sprites);
Tile tile = new Tile(tileName, allowStand, allowFly, stickiness, spriteID, spriteName);
GridTile gridTile = new GridTile(tile, x, y);
this.grid[x] ~= tile;
this.squareGrid[x] ~= gridTile;
assert(this.grid[x][y] == this.squareGrid[x][y].tile);
if ("Unit" in tileData) {
Unit occupyingUnit = this.loadUnitFromJSON(tileData["Unit"], spriteIndex);
occupyingUnit.setLocation(x, y);
} else if ("Player Unit" in tileData) {
this.grid[x][y].startLocation = true;
startingPoints ~= gridTile;
}
}
write("Finished loading row ");
writeln(x);
}
this.mapSizePx.x = cast(int)this.squareGrid.length * TILESIZE;
this.mapSizePx.y = cast(int)this.squareGrid[0].length * TILESIZE;
writeln("Finished loading map " ~ this.name);
{
import std.conv;
writeln("Map is "~to!string(this.grid.length)~" by "~to!string(this.grid.length)~" tiles.");
}
this.gridMarker = LoadTexture("../sprites/grid-marker.png".toStringz);
this.fullyLoaded = true;
}
void run() {
startPreparation();
playerTurn();
}
void startPreparation() {
Rectangle menuBox = {x:0, y:GetScreenHeight()-96, width:GetScreenWidth(), height:96};
Unit[] availableUnits;
UnitCard[Unit] unitCards;
JSONValue playerUnitsData = parseJSON(readText("Units.json"));
writeln("Opened Units.json");
foreach (int k, unitData; playerUnitsData.array) {
Unit unit = loadUnitFromJSON(unitData, spriteIndex, false);
availableUnits ~= unit;
unitCards[unit] = new UnitCard(unit, k*258, GetScreenHeight()-88);
}
writeln("There are "~to!string(unitCards.length)~" units available.");
scope(exit) CloseWindow();
this.phase = GamePhase.Preparation;
auto timer = StopWatch(AutoStart.yes);
bool startButtonAvailable = false;
Vector2 mousePosition = GetMousePosition();
const Vector2 dragOffset = {x: -TILESIZE/2, y: -TILESIZE*0.75 };
this.offset = Vector2(0.0, -96.0);
this.mapView = Rectangle(0, 0, GetScreenWidth, GetScreenHeight-96);
ushort unitsDeployed = 0;
while(!WindowShouldClose()) {
unitsDeployed = 0;
BeginDrawing();
this.offsetMap(mapView);
drawTiles();
foreach(startTile; startingPoints) {
DrawRectangleRec(startTile.getRect, Color(250, 250, 60, 60));
DrawRectangleLinesEx(startTile.getRect, 1.5f, Color(240, 240, 40, 120));
if (startTile.occupant !is null) {
unitsDeployed++;
Vector2 destination = startTile.getOriginSS + Vector2(0, -24);
Color tint;
if (startTile.tile.occupant == this.selectedUnit) tint = Color(250, 250, 250, 190);
else tint = Color(255, 255, 255, 255);
DrawTextureV(this.sprites[startTile.tile.occupant.spriteID], destination, tint);
}
if (CheckCollisionPointRec(mousePosition, startTile.getRect)) {
DrawRectangleRec(startTile.getRect, Color(250, 30, 30, 30));
}
}
drawGridMarkers(timer.peek.total!"msecs");
drawUnits();
DrawRectangleRec(menuBox, Colours.PAPER);
foreach (card; unitCards) if (card.available) {
card.draw();
}
if (IsKeyDown(KeyboardKey.KEY_SPACE)) {
DrawRectangleRec(mapView, Color(250, 20, 20, 50));
}
mousePosition = GetMousePosition();
if (this.selectedUnit is null) {
if (IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) {
bool searching = true;
foreach (card; unitCards) if (CheckCollisionPointRec(mousePosition, card.outerRect)) {
searching = false;
if (card.available) {
this.selectedUnit = &card.unit;
card.available = false;
break;
}
}
if (searching) foreach (startTile; startingPoints) if (CheckCollisionPointRec(mousePosition, startTile.getRect)) {
this.selectedUnit = startTile.tile.occupant;
}
}
} else {
DrawTextureV(this.sprites[this.selectedUnit.spriteID], mousePosition+dragOffset, Colors.WHITE);
if (IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) {
bool deployed;
if (CheckCollisionPointRec(mousePosition, menuBox)) deployed = false;
else deployed = (this.selectedUnit.currentTile !is null);
foreach (gridTile; startingPoints) if (CheckCollisionPointRec(mousePosition, gridTile.getRect)) {
Unit* previousOccupant = gridTile.tile.occupant;
if (gridTile.occupant !is null) {
unitCards[*previousOccupant].available = true;
}
if (this.selectedUnit.currentTile !is null) this.selectedUnit.currentTile.occupant = null;
gridTile.tile.occupant = this.selectedUnit;
this.selectedUnit.currentTile = &gridTile.tile;
unitCards[*selectedUnit].available = false;
deployed = true;
writeln("Unit "~gridTile.tile.occupant.name~" is being deployed.");
if (previousOccupant !is null) previousOccupant.currentTile = null;
this.selectedUnit = previousOccupant;
break;
}
if (!deployed) {
unitCards[*selectedUnit].available = true;
if (this.selectedUnit.currentTile !is null) {
this.selectedUnit.currentTile.occupant = null;
this.selectedUnit.currentTile = null;
}
this.selectedUnit = null;
}
}
}
if (unitsDeployed > 0 && timer.peek() >= msecs(1000*startingPoints.length/unitsDeployed)) {
Rectangle startButton = {GetScreenWidth-92, GetScreenHeight-160, 80, 48};
DrawRectangleRec(startButton, Colours.CRIMSON);
DrawText("Start mission".toStringz, GetScreenWidth-92, GetScreenHeight-140, 16, Colours.PAPER);
if (CheckCollisionPointRec(mousePosition, startButton) && IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) {
this.nextTurn;
break;
};
}
EndDrawing();
}
foreach (startTile; this.startingPoints) {
startTile.occupant.map = this;
writeln("Just assigned Unit.map to the Mission object.");
this.factionUnits["player"] ~= *startTile.occupant;
}
this.startingPoints = [];
}
void playerTurn() {
this.turnReset();
//scope(exit) CloseWindow();
auto missionTimer = StopWatch(AutoStart.yes);
Vector2 mousePosition = GetMousePosition();
Vector2 highlightedTile;
bool oscDirection = false;
ubyte markerOpacity;
while(!WindowShouldClose())
{
BeginDrawing();
mousePosition = GetMousePosition();
if (oscDirection) {
markerOpacity += 2;
if (markerOpacity == 128) oscDirection = false;
} else {
markerOpacity -= 2;
if (markerOpacity == 32) oscDirection = true;
}
drawTiles();
foreach (int gridx, row; this.squareGrid) {
foreach (int gridy, gridTile; row) {
if (CheckCollisionPointRec(mousePosition, gridTile.getRect)) {
if (gridTile.tile.occupant !is null) {
if (IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) {
this.selectedUnit = gridTile.tile.occupant;
this.selectedUnit.updateDistances();
}
if (this.selectedUnit !is null) {
if (this.selectedUnit.getDistance(gridx, gridy).reachable) {
DrawRectangleRec(gridTile.getRect, Color(100, 100, 245, 32));
}
}
}
DrawRectangleRec(gridTile.getRect, Color(245, 245, 245, 32));
}
DrawTextureEx(gridMarker, Vector2(gridx*TILESIZE, gridy*TILESIZE), 0.0, 1.0, Color(10,10,10, markerOpacity));
}
}
drawUnits();
EndDrawing();
}
CloseWindow();
}
void drawTiles() {
foreach (int x, row; this.squareGrid) {
foreach (int y, gridTile; row) {
DrawTextureV(gridTile.sprite, gridTile.getOriginSS, Colors.WHITE);
}
}
}
void drawGridMarkers(long time) {
import std.math.trigonometry:sin;
float sinwave = 60*(sin(cast(float)time/300.0f)+1.0);
int opacity = sinwave.to!int + 20;
foreach (int x, row; this.squareGrid) {
foreach (int y, gridTile; row) {
DrawTextureV(this.gridMarker, gridTile.getOriginSS, Color(10,10,10, cast(ubyte)sinwave));
}
}
}
void drawUnits() {
foreach (unit; this.allUnits) {
Vector2 origin = {x: unit.xlocation*TILESIZE+this.offset.x, y: unit.ylocation*TILESIZE+this.offset.y-24};
DrawTextureV(this.sprites[unit.spriteID], origin, Colors.WHITE);
}
}
void drawOnMap(Texture2D sprite, Rectangle rect) {
Vector2 destination = rectDest(rect, this.offset);
DrawTextureRec(sprite, rect, destination, Colors.WHITE);
}
void drawOnMap(Rectangle rect, Color colour) {
rect.x += this.offset.x;
rect.y += this.offset.y;
DrawRectangleRec(rect, colour);
}
void offsetMap(Rectangle mapView) {
Vector2 offsetOffset;
Vector2 SECornerSS = this.squareGrid[$-1][$-1].SECornerSS + this.offset;
if (IsMouseButtonDown(MouseButton.MOUSE_BUTTON_RIGHT)) {
offsetOffset = GetMouseDelta();
} else {
float framelength = GetFrameTime();
if (IsKeyDown(KeyboardKey.KEY_A)) {
offsetOffset.x = -framelength * 32.0;
}
if (IsKeyDown(KeyboardKey.KEY_D)) {
offsetOffset.x = framelength * 32.0;
}
if (IsKeyDown(KeyboardKey.KEY_W)) {
offsetOffset.y = -framelength * 32.0;
}
if (IsKeyDown(KeyboardKey.KEY_S)) {
offsetOffset.y = framelength * 32.0;
}
}
this.offset += offsetOffset;
if (offset.x > mapView.x) offset.x = 0.0f;
else if (offset.x + mapSizePx.x < mapView.width) offset.x = mapView.width - mapSizePx.x;
if (offset.y > mapView.y) offset.y = 0.0f;
else if (offset.y + mapSizePx.y < mapView.height) offset.y = mapView.height - mapSizePx.y;
}
Unit loadUnitFromJSON (JSONValue unitData, ref int[string] spriteIndex, bool addToMap=true) {
Unit newUnit;
if (addToMap) newUnit = new Unit(this, unitData);
else newUnit = new Unit(unitData);
string spriteName = unitData["Sprite"].get!string;
if (spriteName in spriteIndex) {
newUnit.spriteID = spriteIndex[spriteName];
} else {
newUnit.spriteID = cast(uint)this.sprites.length;
assert(newUnit.spriteID > 0);
writeln("Player unit spriteID = "~to!string(newUnit.spriteID));
spriteIndex[spriteName] = newUnit.spriteID;
string spritePath = ("../sprites/units/" ~ spriteName).buildNormalizedPath;
if (!spritePath.endsWith(".png")) spritePath ~= ".png";
this.sprites ~= LoadTexture(spritePath.toStringz);
}
//allUnits ~= newUnit; Removed because this was added to Unit.this
return newUnit;
}
class GridTile
{
Tile tile;
private Vector2i origin;
int x;
int y;
this(Tile tile, int x, int y) {
this.tile = tile;
this.origin = Vector2i(x*TILESIZE, y*TILESIZE);
this.x = x;
this.y = y;
}
Rectangle getRect() {
float x = this.origin.x + this.outer.offset.x;
float y = this.origin.y + this.outer.offset.y;
return Rectangle(x:x, y:y, width:TILESIZE, height:TILESIZE);
}
Vector2i getOriginAbs() {
return this.origin;
}
Vector2 getOriginSS() {
float x = this.origin.x + this.outer.offset.x;
float y = this.origin.y + this.outer.offset.y;
return Vector2(x, y);
}
Vector2 SECornerSS() {
float x = this.origin.x + TILESIZE + this.outer.offset.x;
float y = this.origin.y + TILESIZE + this.outer.offset.y;
return Vector2(x, y);
}
Unit* occupant() {
return this.tile.occupant;
}
int spriteID() {
return cast(int)this.tile.textureID;
}
Texture2D sprite() {
return this.outer.sprites[this.tile.textureID];
}
}
class UnitCard
{
Rectangle outerRect;
Rectangle imageFrame;
int x;
int y;
int width;
int height;
Unit unit;
bool available = true;
string infotext;
this (Unit unit, int screenx, int screeny ) {
this.outerRect = Rectangle(screenx, screeny, 192, 80);
this.imageFrame = Rectangle(screenx+4, screeny+4, 64, 64);
this.unit = unit;
this.x = screenx;
this.y = screeny;
this.width = 256;
this.height = 72;
UnitStats stats = unit.getStats;
this.infotext ~= "Mv: "~to!string(stats.Mv)~"\n";
this.infotext ~= "MHP: "~to!string(stats.MHP)~"\n";
this.infotext ~= "Str: "~to!string(stats.Str)~"\n";
this.infotext ~= "Def: "~to!string(stats.Def)~"\n";
}
UnitStats stats() {
return this.unit.getStats;
}
void draw() {
DrawRectangleRec(outerRect, Color(r:250, b:230, g:245, a:200));
DrawRectangleLinesEx(outerRect, 1.0f, Colors.BLACK);
DrawTexture(this.outer.sprites[this.unit.spriteID], cast(int)outerRect.x+4, cast(int)outerRect.y+2, Colors.WHITE);
DrawText(this.unit.name.toStringz, x+80, y+4, 14, Colors.BLACK);
DrawText(this.infotext.toStringz, x+80, y+20, 11, Colors.BLACK);
}
}
}
ushort loadNumberTexture (string spriteName, ref int[string] spriteIndex, ref Texture2D[] sprites) {
ushort spriteID;
if (spriteName in spriteIndex) {
spriteID = cast(ushort)spriteIndex[spriteName];
} else {
string spritePath = ("../sprites/tiles/" ~ spriteName).buildNormalizedPath;
spriteID = cast(ushort)sprites.length;
sprites ~= LoadTexture(spritePath.toStringz);
spriteIndex[spriteName] = spriteID;
}
return spriteID;
}
enum Colours {
SHADOW = Color(r:20, b:20, g:20, a:25),
PAPER = Color(r:240, b:210, g:234, a:250),
SHINE = Color(250, 250, 60, 35),
CRIMSON = Color(230, 10, 15, 255),
}
unittest
{
validateRaylibBinding();
Mission mission = new Mission("../maps/test-map.json");
writeln("Mission unittest: Finished Mission constructor.");
foreach (unit; mission.allUnits) {
assert(unit.map == mission);
if(mission != unit.map) writeln("These objects do not match");
}
}