Skip to content

Commit 2ca86c8

Browse files
Refactor, Remove Randomness, no more explosions and more (#15)
* update tsconfigs, missing fn calls * disable explosions * as for ticks from horse's mouth * remove all Math.random() * remove logs * add new deployment.json
1 parent 0c673fe commit 2ca86c8

10 files changed

+127
-152
lines changed

client/game/comets.ts

+6-40
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { removeFromStore, StorageKey } from "../rpc/storage";
44
import { getWalletClient } from "../rpc/wallet";
55
import { AttractMode } from "./attractMode";
66
import { GameMode } from "./gameMode";
7-
import Global from "./global";
8-
import { Key, Keys } from "./keys";
7+
import { Key } from "./keys";
98
import { loop } from "./loop";
109
import { Screen } from "./screen";
11-
import { Sound } from "./sounds";
1210
import { TickRecorder } from "./tickRecorder";
1311
import { World } from "./world";
1412

@@ -32,17 +30,17 @@ export class Comets {
3230
this.tickRecorder = new TickRecorder();
3331

3432
const setGameMode = () => {
35-
this.gameMode = new GameMode(new World());
36-
this.currentMode = this.gameMode;
3733
this.tickRecorder.reset();
34+
this.gameMode = new GameMode(new World(), this.tickRecorder);
35+
this.currentMode = this.gameMode;
3836

39-
this.gameMode.on("done", async (source, world) => {
37+
this.gameMode.on("done", (source, world) => {
4038
// Send ticks in the form of an action to MRU
4139
// And wait for C1 to confirm score
4240
this.lastScore = world.score;
4341
if (!this.isSendingTicks) {
4442
this.isSendingTicks = true;
45-
await this.tickRecorder
43+
this.tickRecorder
4644
.sendTicks(this.lastScore)
4745
.then(() => {
4846
console.log("Sent ticks");
@@ -53,13 +51,13 @@ export class Comets {
5351
.finally(() => {
5452
removeFromStore(StorageKey.GAME_ID);
5553
this.isSendingTicks = false;
54+
console.log("Game over");
5655
this.init();
5756
// Reload page
5857
window.location.reload();
5958
});
6059
}
6160
// restart in attract mode
62-
console.log("Game over");
6361
});
6462
};
6563

@@ -70,39 +68,7 @@ export class Comets {
7068
}
7169

7270
update(dt) {
73-
if (Key.wasPressed(Keys.GOD)) {
74-
Global.god = !Global.god;
75-
}
76-
77-
if (Key.wasPressed(Keys.DEBUG)) {
78-
Global.debug = !Global.debug;
79-
}
80-
81-
if (Key.wasPressed(Keys.MONITOR_BURN)) {
82-
Global.burn = !Global.burn;
83-
}
84-
85-
if (Key.wasPressed(Keys.PAUSE)) {
86-
Global.paused = !Global.paused;
87-
88-
if (Global.paused) {
89-
Sound.off();
90-
} else {
91-
Sound.on();
92-
}
93-
}
94-
95-
if (Global.paused) {
96-
return;
97-
}
98-
9971
const gameInputs = this.tickRecorder.collectInputs();
100-
101-
// We only record ticks in game mode
102-
if (this.currentMode === this.gameMode) {
103-
this.tickRecorder.recordInputs(gameInputs);
104-
}
105-
10672
this.currentMode.update(dt, gameInputs);
10773
}
10874

client/game/explosion.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class Explosion extends EventSource {
2020
super();
2121

2222
for (let i = 0; i < 15; i++) {
23-
const v = Vector.fromAngle(random(1, 360), Math.random() * VELOCITY);
24-
this.points.push({ x: x, y: y, vx: v.x, vy: v.y, alpha: Math.random() });
23+
const v = Vector.fromAngle(random(1, 360), random(0, 1) * VELOCITY);
24+
this.points.push({ x: x, y: y, vx: v.x, vy: v.y, alpha: random(0, 1) });
2525
}
2626
}
2727

client/game/gameMode.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ export const ACTIONS = [
1818
"wasHyperspace",
1919
];
2020

21+
const WAIT_TIME = 5;
22+
2123
export class GameMode extends EventSource implements IGameState {
2224
bounds: Object2D[] = [];
2325
thumper: Thumper;
26+
tickRecorder = {
27+
recordInputs: (ticks) => {},
28+
};
2429

2530
private lastCollisions: Collisions;
2631

27-
constructor(private world: World) {
32+
constructor(private world: World, tickRecorder) {
2833
super();
34+
if (tickRecorder) {
35+
this.tickRecorder = tickRecorder;
36+
}
2937
}
3038

3139
init() {
@@ -44,6 +52,7 @@ export class GameMode extends EventSource implements IGameState {
4452
}
4553

4654
update(dt: number, inputs?: VirtualInput) {
55+
this.tickRecorder.recordInputs(inputs);
4756
this.world.levelTimer += dt;
4857

4958
if (this.thumper && this.world.ship) {
@@ -53,7 +62,7 @@ export class GameMode extends EventSource implements IGameState {
5362
if (this.world.gameOver) {
5463
this.world.gameOverTimer += dt;
5564

56-
if (this.world.gameOverTimer >= 5) {
65+
if (this.world.gameOverTimer >= WAIT_TIME) {
5766
this.trigger("done", this.world);
5867
}
5968
}

client/game/screen.ts

+81-82
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,96 @@
1-
import { Draw } from './draw';
2-
import { Rect } from '../comets';
1+
import { Rect } from "../comets";
32
import { HEIGHT, OBJECT_SCALE, SHIP_RECT, WIDTH } from "./constants";
3+
import { Draw } from "./draw";
4+
import { random } from "./util";
45

56
export class Screen implements Rect {
7+
canvas: HTMLCanvasElement;
8+
ctx: CanvasRenderingContext2D;
9+
x: number = 0;
10+
y: number = 0;
11+
width: number;
12+
height: number;
13+
draw: Draw;
14+
width2: number;
15+
height2: number;
616

7-
canvas: HTMLCanvasElement;
8-
ctx: CanvasRenderingContext2D;
9-
x: number = 0;
10-
y: number = 0;
11-
width: number;
12-
height: number;
13-
draw: Draw;
14-
width2: number;
15-
height2: number;
16-
17-
private _fontXL: number;
18-
private _fontL: number;
19-
private _fontM: number;
20-
private _fontS: number;
21-
private _objectScale: number;
22-
private _shipRect: Rect;
23-
private _pointSize: number;
17+
private _fontXL: number;
18+
private _fontL: number;
19+
private _fontM: number;
20+
private _fontS: number;
21+
private _objectScale: number;
22+
private _shipRect: Rect;
23+
private _pointSize: number;
2424

25-
constructor() {
26-
this.canvas = document.getElementById('canvas') as HTMLCanvasElement;
27-
this.ctx = this.canvas.getContext('2d');
28-
this.draw = new Draw(this.ctx, this);
29-
this.init();
25+
constructor() {
26+
this.canvas = document.getElementById("canvas") as HTMLCanvasElement;
27+
this.ctx = this.canvas.getContext("2d");
28+
this.draw = new Draw(this.ctx, this);
29+
this.init();
3030

31-
window.addEventListener('resize', () => {
32-
console.log("resizing");
33-
this.init();
34-
});
35-
}
31+
window.addEventListener("resize", () => {
32+
console.log("resizing");
33+
this.init();
34+
});
35+
}
3636

37-
init() {
38-
this.canvas.width = WIDTH;
39-
this.canvas.height = HEIGHT;
40-
this.width = this.canvas.width;
41-
this.height = this.canvas.height;
42-
this.width2 = this.width / 2;
43-
this.height2 = this.height / 2;
37+
init() {
38+
this.canvas.width = WIDTH;
39+
this.canvas.height = HEIGHT;
40+
this.width = this.canvas.width;
41+
this.height = this.canvas.height;
42+
this.width2 = this.width / 2;
43+
this.height2 = this.height / 2;
4444

45-
this._fontXL = 48;
46-
this._fontL = 24;
47-
this._fontM = 18;
48-
this._fontS = 10;
49-
this._objectScale = OBJECT_SCALE;
45+
this._fontXL = 48;
46+
this._fontL = 24;
47+
this._fontM = 18;
48+
this._fontS = 10;
49+
this._objectScale = OBJECT_SCALE;
5050

51-
this._pointSize = 4 * this._objectScale;
51+
this._pointSize = 4 * this._objectScale;
5252

53-
this._shipRect = SHIP_RECT;
54-
}
53+
this._shipRect = SHIP_RECT;
54+
}
5555

56-
get font() {
57-
let self = this;
58-
return {
59-
get xlarge() {
60-
return self._fontXL;
61-
},
62-
get large() {
63-
return self._fontL;
64-
},
65-
get medium() {
66-
return self._fontM;
67-
},
68-
get small() {
69-
return self._fontS;
70-
}
71-
}
72-
}
56+
get font() {
57+
let self = this;
58+
return {
59+
get xlarge() {
60+
return self._fontXL;
61+
},
62+
get large() {
63+
return self._fontL;
64+
},
65+
get medium() {
66+
return self._fontM;
67+
},
68+
get small() {
69+
return self._fontS;
70+
},
71+
};
72+
}
7373

74-
get objectScale() {
75-
return this._objectScale;
76-
}
77-
78-
get pointSize() {
79-
return this._pointSize;
80-
}
81-
82-
get shipRect() {
83-
return this._shipRect;
84-
}
74+
get objectScale() {
75+
return this._objectScale;
76+
}
8577

86-
preShake() {
87-
this.ctx.save();
88-
var dx = Math.random() * 10;
89-
var dy = Math.random() * 10;
90-
this.ctx.translate(dx, dy);
91-
}
78+
get pointSize() {
79+
return this._pointSize;
80+
}
9281

93-
postShake() {
94-
this.ctx.restore();
95-
}
96-
}
82+
get shipRect() {
83+
return this._shipRect;
84+
}
85+
86+
preShake() {
87+
this.ctx.save();
88+
var dx = random(0, 1) * 10;
89+
var dy = random(0, 1) * 10;
90+
this.ctx.translate(dx, dy);
91+
}
9792

93+
postShake() {
94+
this.ctx.restore();
95+
}
96+
}

client/game/tickRecorder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ export class TickRecorder {
2323
inputMap["isThrust"] = true;
2424
}
2525

26-
if (Key.wasRotateLeft) {
26+
if (Key.wasRotateLeft()) {
2727
inputMap["wasRotateLeft"] = true;
2828
}
2929

3030
if (Key.isRotateLeft()) {
3131
inputMap["isRotateLeft"] = true;
3232
}
3333

34-
if (Key.wasRotateRight) {
34+
if (Key.wasRotateRight()) {
3535
inputMap["wasRotateRight"] = true;
3636
}
3737

client/game/world.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,28 @@ export class World {
166166
if (explosionCount > maxExplosionCount) {
167167
maxExplosionCount = explosionCount;
168168

169-
if (maxExplosionCount > maxExplosionThreshold) {
170-
explosionCount = 0;
171-
console.log("MAX DAMAGE ACHEIVEMENT");
169+
// TODO: this was exploding the game
170+
// if (maxExplosionCount > maxExplosionThreshold) {
171+
// explosionCount = 0;
172+
// console.log("MAX DAMAGE ACHEIVEMENT");
172173

173-
this.setSlowMo(0.25, 4);
174+
// this.setSlowMo(0.25, 4);
174175

175-
let bonus = 0;
176-
explosionScores.forEach((v) => (bonus += v));
177-
bonus *= 5;
176+
// let bonus = 0;
177+
// explosionScores.forEach((v) => (bonus += v));
178+
// bonus *= 5;
178179

179-
const achievement = new Achievement(`MASSIVE DAMAGE`, bonus);
180-
this.addScenery(achievement);
181-
this.addScore(achievement, "achievement");
180+
// const achievement = new Achievement(`MASSIVE DAMAGE`, bonus);
181+
// this.addScenery(achievement);
182+
// this.addScore(achievement, "achievement");
182183

183-
//const marker = new ScoreMarker(obj, `+${bonus}`);
184-
//this.addScenery(marker);
184+
// //const marker = new ScoreMarker(obj, `+${bonus}`);
185+
// //this.addScenery(marker);
185186

186-
// Track score of each explosion and display total points with achievement
187+
// // Track score of each explosion and display total points with achievement
187188

188-
maxExplosionThreshold += 10;
189-
}
189+
// maxExplosionThreshold += 10;
190+
// }
190191
}
191192

192193
explosion.on("expired", () => {

client/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"removeComments": true,
66
"sourceMap": true
77
},
8-
"exclude": [
9-
"rollup/**"
8+
"include": [
9+
"game/*.ts",
10+
"rpc/*.ts"
1011
]
1112
}

0 commit comments

Comments
 (0)