This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameloop.js
145 lines (117 loc) · 6.9 KB
/
gameloop.js
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
//schedule to update all games every minute on the 0th second
var schedule = require('node-schedule');
var Game = require('./models/game');
var Pathfinding = require('pathfinding');
var enemyCloseRange = require('./entities/enemies/enemy_close_range');
var modeSurvival = require('./entities/modes/mode_survival');
console.log('init game loop');
var gameloop = function(io){
schedule.scheduleJob('0 * * * * *', function(){
console.log('scheduled job is running');
Game.getActiveGames(function(err, activeGames){
//for all active games
for (var i = 0; i < activeGames.length; i++){
//create a grid to represent this game's grid
var grid = new Pathfinding.Grid(modeSurvival.mapSize, modeSurvival.mapSize);
//initialize finder algorithm
var finder = new Pathfinding.AStarFinder();
//TODO: there is a huge glitch where if you surround your base in towers you'll win. What do? Have enemies attack towers that have attacked them probably?
// Or just not put the towers as unwalkable and if an enemy is about to land on one they attack it instead?
//TODO: Have each tower attack a near enemy here
//TODO: if that enemy died, -1 their helath
//TODO: Go through enemy list, remove all enemies with 0 health from list
//for each enemy that's left, populate the grid with enemy locations - mark them as "unwalkable"
for(var j = 0; j < activeGames[i].enemies.length; j++){
console.log('set up grid', activeGames[i].enemies[j].positionX, activeGames[i].enemies[j].positionY);
grid.setWalkableAt(activeGames[i].enemies[j].positionX, activeGames[i].enemies[j].positionY, false);
}
//for each enemy
for(var k = 0; k < activeGames[i].enemies.length; k++){
//have to make a copy of the grid as running finder kills it for some reason. See docs.
var usableCopyOfGrid = grid.clone();
//figure out that enemy's path to the center
var path = finder.findPath(activeGames[i].enemies[k].positionX, activeGames[i].enemies[k].positionY, 2, 2, usableCopyOfGrid);
//note: path[0] is your current position, so path[1] is the next positon
console.log('path', path);
if(path.length === 0){
// if there is no way to move to get to the center, don't do anything
// intentionally left empty
console.log('path length is 0!')
//TODO: } else if (enemy is about to step on a tower) { have it attack said tower }
} else if(path[1][0] === 2 && path[1][1] === 2){
// else, if the enemy is already moved within range of a target
// (currently only the main building, hard coded at [2][2]),
// have it attack said target
activeGames[i].baseHealth = activeGames[i].baseHealth - enemyCloseRange.damage;
console.log('attacked main building!')
} else if(path.length === 1){
console.log('THIS SHOULD NEVER HAPPEN, it means that an enemy is probably ON the base', path);
} else{
// else, the enemy should move toward the target
console.log('should move to', path[1][0], path[1][1]);
//remove the enemy from the grid position it is currently at on the grid, making it walkable again
grid.setWalkableAt(activeGames[i].enemies[k].positionX, activeGames[i].enemies[k].positionY, true);
//make the grid position the enemy is going to unwalkable
grid.setWalkableAt(path[1][0], path[1][1], false);
//make the change to the enemy's position in our array
activeGames[i].enemies[k].positionX = path[1][0];
activeGames[i].enemies[k].positionY = path[1][1];
console.log('new position', activeGames[i].enemies[k].positionX, activeGames[i].enemies[k].positionY)
}
}
//mark the game as done if their base is dead
if (activeGames[i].baseHealth <= 0){
activeGames[i].isGameOver = true;
} else{
// If the game isn't done
// then add a new enemy to the board
//choose x and y where enemy should spawn
//currently only spawning in corners for no good reason
var positionX;
var positionY;
if(Math.random() >= 0.5){
positionX = 0;
} else{
positionX = modeSurvival.mapSize - 1;
}
if(Math.random() >= 0.5){
positionY = 0;
} else{
positionY = modeSurvival.mapSize - 1;
}
//make sure it isn't where an enemy is already
if(grid.nodes[positionX][positionY].walkable){
// if it isn't where an enemy is already, spawn it
//defaulting enemy to enemyCloseRange for now
//can probably do this cleaner with the "new" keyword?
activeGames[i].enemies.push({
type: enemyCloseRange.name,
health: enemyCloseRange.maxHealth,
positionX: positionX,
positionY: positionY
});
} else{
// if it is where an enemy is already, just don't spawn anything, becuase we are lazy
}
//Add resources to your pool, based on your game type
activeGames[i].resources += modeSurvival.resourcesPerRound;
//iterate round number
activeGames[i].round += 1;
}
activeGames[i].save(function (err, updatedGame){
if (err) {
console.log(err);
} else{
console.log('updated game', updatedGame);
}
});
}
});
//Now that the game loop has run and all things are saved,
//send a socket.io message to all clients with their updated info
//TODO make it actually sent the stuffs
io.sockets.emit('gameLoopFinished', '');
});
};
// make this available to our users in our Node applications
module.exports = gameloop;