-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameengine.js
More file actions
155 lines (138 loc) · 4.5 KB
/
Copy pathgameengine.js
File metadata and controls
155 lines (138 loc) · 4.5 KB
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
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
};
})();
function GameEngine() {
this.entities = [];
this.ctx = null;
this.surfaceWidth = null;
this.surfaceHeight = null;
}
GameEngine.prototype.init = function (ctx) {
this.ctx = ctx;
this.surfaceWidth = this.ctx.canvas.width;
this.surfaceHeight = this.ctx.canvas.height;
this.timer = new Timer();
console.log('game initialized');
};
GameEngine.prototype.start = function () {
console.log("starting game");
var that = this;
(function gameLoop() {
that.loop();
requestAnimFrame(gameLoop, that.ctx.canvas);
})();
};
GameEngine.prototype.addEntity = function (entity) {
console.log('added entity');
this.entities.push(entity);
};
GameEngine.prototype.removeEntity = function (entity) {
var location = this.entities.indexOf(entity);
this.entities.splice(location, 1);
};
GameEngine.prototype.draw = function () {
this.ctx.clearRect(0, 0, this.surfaceWidth, this.surfaceHeight);
this.ctx.save();
for (var i = 0; i < this.entities.length; i++) {
this.entities[i].draw(this.ctx);
}
this.ctx.restore();
};
GameEngine.prototype.update = function () {
for (var i = 0; i < this.entities.length; i++) {
var entity = this.entities[i];
if (!entity.removeFromWorld) {
entity.update();
if (!entity.collapsing) entity.detectCollision();
} else {
this.removeEntity(entity);
if (entity instanceof Food) {
var chance = Math.random();
if (this.entities.length < 260) chance = 1;
if (chance > .95) this.addEntity(new Food(this));
}
}
}
// for (var i = 0; i < entitiesCount; i++) {
// var entity = this.entities[i];
// entity.detectCollision();
// }
};
GameEngine.prototype.loop = function () {
this.clockTick = this.timer.tick();
this.update();
this.draw();
};
GameEngine.prototype.save = function () {
var rtn = [];
for (var i = 0; i < this.entities.length; i++) {
var ent = this.entities[i];
if (ent instanceof Food) {
var entry = {type: "Food", x: ent.x, y: ent.y};
rtn.push(entry);
} else {
var entry = {type: "Cell", x: ent.x, y: ent.y, radius: ent.radius, vX: ent.vX, vY: ent.vY,
collapsing: ent.collapsing, collapseCycle: ent.collapseCycle, inert: ent.inert, criticalMassCycles: ent.criticalMassCycles,
maxRadius: ent.maxRadius };
console.log(entry);
rtn.push(entry);
}
}
return rtn;
};
GameEngine.prototype.load = function (entities) {
this.entities = [];
for (var i = 0; i < entities.length; i++) {
var ent = entities[i];
if (ent.type === "Food") {
this.addEntity(new Food(this, ent.x, ent.y));
} else {
var cell = new Cell(this, ent.x, ent.y, ent.radius, true);
cell.vX = ent.vX;
cell.vY = ent.vY;
cell.collapsing = ent.collapsing;
cell.collapseCycle = ent.collapseCycle;
cell.criticalMassCycles = ent.criticalMassCycles;
cell.inert = ent.inert;
cell.maxRadius = ent.maxRadius;
this.addEntity(cell);
}
}
};
function Timer() {
this.gameTime = 0;
this.maxStep = 0.05;
this.wallLastTimestamp = 0;
}
Timer.prototype.tick = function () {
var wallCurrent = Date.now();
var wallDelta = (wallCurrent - this.wallLastTimestamp) / 1000;
this.wallLastTimestamp = wallCurrent;
var gameDelta = Math.min(wallDelta, this.maxStep);
this.gameTime += gameDelta;
return gameDelta;
};
function Entity(game, x, y) {
this.game = game;
this.x = x;
this.y = y;
this.removeFromWorld = false;
}
Entity.prototype.update = function () {
};
Entity.prototype.draw = function (ctx) {
if (this.game.showOutlines && this.radius) {
this.game.ctx.beginPath();
this.game.ctx.strokeStyle = "green";
this.game.ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
this.game.ctx.stroke();
this.game.ctx.closePath();
}
};