-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.js
161 lines (144 loc) · 4.67 KB
/
base.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
157
158
159
160
161
/* base.js contains a function to search a random location on the map for a new base,
* a function refuel() to regain energy and shield when inside a base,
* and several functions to add a new base to the game.
*/
/* global player, shapesData
* MAP_WIDTH, MAP_HEIGHT, TANK_WIDTH, TANK_HEIGHT, TANK_MAX_ENERGY, TANK_MAX_HEALTH,
* collision, digBase
*/
const BASE_WIDTH = 40;
const BASE_HEIGHT = 40;
// List of bases
const bases = [];
// Generate a random location for a new base
function randomBaseLocation(id) {
let rect;
do {
rect = {
id: id,
x: Math.floor(Math.random() * MAP_WIDTH - (BASE_WIDTH * 2)),
y: Math.floor(Math.random() * MAP_HEIGHT - (BASE_HEIGHT * 2)),
w: BASE_WIDTH,
h: BASE_HEIGHT
};
} while (collision(rect));
return rect;
}
// Check if inside a base, to refuel/repair
function refuel() {
let fueled = false;
for (const base of bases) {
// Am I in a base?
if (player.x >= base.x
&& player.y >= base.y
&& player.x + TANK_WIDTH <= base.x + base.w
&& player.y + TANK_HEIGHT <= base.y + base.h
&& (! (player.x < base.x + 15 && player.y < base.y + 15))
&& (! (player.x > base.x + 25 && player.y < base.y + 15))
&& (! (player.x < base.x + 15 && player.y > base.y + 25))
&& (! (player.x > base.x + 25 && player.y > base.y + 25))) {
// My own base?
if (player.id == base.id) {
if (player.energy < TANK_MAX_ENERGY) {
player.energy += 5;
fueled = true;
}
if (player.health < TANK_MAX_HEALTH) {
player.health += 0.2;
fueled = true;
}
// Some elses base?
} else {
if (player.energy < TANK_MAX_ENERGY) {
player.energy += 3;
fueled = true;
}
}
if (player.energy > TANK_MAX_ENERGY) {
player.energy = TANK_MAX_ENERGY;
}
if (player.health > TANK_MAX_HEALTH) {
player.health = TANK_MAX_HEALTH;
}
}
}
return fueled;
}
// Dig a base and add the walls as blocking objects on the map
function addBase(base) {
bases.push(base);
digBase(base);
blockBaseWalls(base.x, base.y);
}
// Add the walls of the base at this location in shapesData (map of all shapes that block movement and bullets)
function blockBaseWalls(x, y) {
const data = baseDigData();
for (let ry = 0; ry < BASE_HEIGHT; ry++) {
for (let rx = 0; rx < BASE_WIDTH; rx++) {
if (data[ry][rx] == '2') {
let a = ((ry + y) * MAP_WIDTH * 4) + ((rx + x) * 4) + 3;
shapesData[a] = 255;
}
}
}
}
// Draw picture of the base on the canvas context
function drawBaseWalls(ctx, base, x, y) {
const data = baseDigData();
// Draw the walls
ctx.fillStyle = 'lightgreen';
for (let ry = 0; ry < BASE_HEIGHT; ry++) {
for (let rx = 0; rx < BASE_WIDTH; rx++) {
if (data[ry][rx] == '2') {
ctx.fillRect(x + rx, y + ry, 1, 1);
}
}
}
// Different roof colors for the different players
if (parseInt(base.id) % 4 == 1) {
ctx.fillStyle = 'lightblue';
} else if (parseInt(base.id) % 4 == 2) {
ctx.fillStyle = 'pink';
} else if (parseInt(base.id) % 4 == 3) {
ctx.fillStyle = 'cyan';
} else {
ctx.fillStyle = 'red';
}
// Draw the roof
for (let ry = 0; ry < BASE_HEIGHT; ry++) {
for (let rx = 0; rx < BASE_WIDTH; rx++) {
if (data[ry][rx] == '3') {
ctx.fillRect(x + rx, y + ry, 1, 1);
}
}
}
}
// This is the pixel data of the base image. 0 = sand, 1 = dug, 2 = wall, 3 = roof
// Because the picture is symmetrical, we define only the top half, and concatenate
// the reversal to get the entire picture.
function baseDigData() {
const tophalf = [
'0000000000000221111111111220000000000000',
'0000000000002211111111111122000000000000',
'0000000000022111111111111112200000000000',
'0000000000221111111111111111220000000000',
'0000000002211111111111111111122000000000',
'0000000022111111111111111111112200000000',
'0000000221111111111111111111111220000000',
'0000002211111111111111111111111122000000',
'0000022131111111111111111111111312200000',
'0000221113111111111111111111113111220000',
'0002211111311111111111111111131111122000',
'0022111111131111111111111111311111112200',
'0221111111113111111111111113111111111220',
'2211111111111311111111111131111111111122',
'2111111111111131111111111311111111111112',
'1111111111111113111111113111111111111111',
'1111111111111111311111131111111111111111',
'1111111111111111131111311111111111111111',
'1111111111111111113333111111111111111111',
'1111111111111111113333111111111111111111'
];
const bottomhalf = tophalf.slice().reverse();
return tophalf.concat(bottomhalf);
}