-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
156 lines (121 loc) · 4.64 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
import {SquareFormation} from './src/game/formations.js';
import {Canvas2D} from './src/canvas/canvas.js';
import Universe from './src/game/universe.js';
import Camera from './src/game/camera.js';
import {Player} from './src/game/player.js';
import {AttackVessel} from './src/game/entities/vessel.js';
import {Turret} from './src/game/entities/turrets.js';
import {LaserTurret} from './src/game/entities/laser.js';
import Star from './src/game/entities/stars.js';
import {Planet} from './src/game/entities/planets.js';
import GameLoop from './src/game/loop.js';
import {LockOnTargetCommand, MoveCommand} from './src/game/command.js';
import {AUDIO_COMMANDS} from './src/game/audio.js';
const debug = document.querySelector('.app > .debug');
const screen = document.querySelector('.app > canvas');
const formation = new SquareFormation();
const canvas = new Canvas2D(document.querySelector('.app > canvas'));
canvas.width = screen.clientWidth;
canvas.height = screen.clientHeight;
canvas.clearColor = 'rgb(28,26,26)';
const universe = new Universe(canvas);
universe.camera = new Camera(universe);
universe.player = new Player(universe);
universe.camera.origin.scale = 0.5;
const enemyPlayer = new Player(universe);
enemyPlayer.color = 'rgb(255,0,0)';
universe.players.push(enemyPlayer);
const yourVessel = new AttackVessel(universe.player);
yourVessel.mountTurret(new Turret(universe.player));
yourVessel.mountTurret(new LaserTurret(universe.player));
yourVessel.position.x = 150;
yourVessel.position.y = -225;
const yourVessel2 = new AttackVessel(universe.player);
yourVessel2.mountTurret(new Turret(universe.player));
yourVessel2.mountTurret(new LaserTurret(universe.player));
yourVessel2.position.x = 200;
yourVessel2.position.y = -200;
const enemyVessel = new AttackVessel(enemyPlayer);
enemyVessel.mountTurret(new Turret(enemyPlayer));
enemyVessel.position.x = -200;
enemyVessel.position.y = 200;
universe.player.add(yourVessel);
universe.player.add(yourVessel2);
enemyPlayer.add(enemyVessel);
const star = new Star(universe.player);
const planet = new Planet(universe.player, 'p01');
planet.position.x = 200;
planet.position.y = -300;
universe.add(star);
universe.add(planet);
universe.debug = false;
canvas.resize();
const gameLoop = new GameLoop();
gameLoop.onUpdate = () => universe.update();
gameLoop.onDraw = () => {
universe.render();
};
gameLoop.onStats = () => {
debug.innerText = `
scale: ${universe.camera.origin.scale}
mouse: ${JSON.stringify(universe.camera.screenToWorld(universe.camera.origin.screenPosition))}
offset: ${JSON.stringify(universe.camera.origin.offset)}
fps: ${Math.round(gameLoop.fps)}
`;
};
document.oncontextmenu = e => {
e.preventDefault();
};
canvas.addEventListener('mousedown', e => {
e.preventDefault();
if (e.which === 3) {
universe.player.selectedUnits.forEach(unit => {
unit.isSelected = false;
});
universe.player.selectedUnits.clear();
}
});
canvas.addEventListener('click', e => {
e.preventDefault();
const screenCoordinates = universe.camera.screenCoordinates(e);
const worldCoordinates = universe.camera.screenToWorld(screenCoordinates);
console.log(
screenCoordinates,
worldCoordinates,
universe.camera.worldToScreen(worldCoordinates)
);
const unit = universe.player.units.findOneByCoordinates(worldCoordinates);
if (unit) {
unit.isSelected = true;
universe.player.selectedUnits.add(unit);
console.log('units selected');
return;
}
if (universe.player.selectedUnits.length === 0) {
return;
}
if (universe.player.selectedUnits.length < 2) {
universe.dispatchCommand(new MoveCommand(universe.player.selectedUnits.at(0), worldCoordinates));
} else {
const destinations = formation.computeDestinations(universe.player.selectedUnits, worldCoordinates);
for (let i in destinations) {
universe.dispatchCommand(new MoveCommand(universe.player.selectedUnits.at(i), destinations[i]));
}
}
let target;
target = universe.player.units.findOneByCoordinates(worldCoordinates);
if (target) {
return;
}
for (let i in universe.players) {
target = universe.players[i].units.findOneByCoordinates(worldCoordinates);
if (target) {
universe.dispatchAudio(AUDIO_COMMANDS.TARGET_CONFIRMED);
universe.dispatchCommand(new LockOnTargetCommand(universe.player.selectedUnits, target));
console.log('units lock on target');
return;
}
}
universe.dispatchAudio(AUDIO_COMMANDS.MOVING_TO_POSITION);
});
gameLoop.start();