-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombat.js
More file actions
199 lines (188 loc) · 5.18 KB
/
combat.js
File metadata and controls
199 lines (188 loc) · 5.18 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
globals = require('./globals.js');
coredata = globals.coredata;
collmap = globals.collmap;
mapchange = globals.mapchange;
attackQueue = globals.attackQueue;
///// Exports ///////////////////////////
module.exports = {
bombcontroller: function () {
bombcontroller();
},
attack: function (attacker, dir) {
attack(attacker, dir);
},
explode: function (bombNumber) {
explode(bombNumber);
},
processBombs: function () {
processBombs();
},
};
function bombcontroller(){
var db = coredata.bombs;
removes = [];
for (var bomb = coredata.bombs.length -1; bomb >= 0; bomb--){
console.log(JSON.stringify(db[bomb]));
if (db[bomb].state <= 3){ removes.push(bomb); break};
if (db[bomb].state < 6){
explode(bomb);
};
if (db[bomb].state > 0){
db[bomb].state -= 1;
};
};
for (var rem in removes){
db.splice(rem, 1)
};
}
function processBombs(){
for (var inst in attackQueue){
attack(attackQueue[inst][0], attackQueue[inst][1])
delete attackQueue[inst];
}
//attackQueue = {};
}
function attack(attacker, npcsORplayers){
// second argument, npc or player is the attribute of the attacker, not whats being attacked.
at = coredata[npcsORplayers];
//coredata.attacks["a" + attacker] = at[attacker].pos;
atdir = at[attacker].dir;
atorig = at[attacker].pos.split(".");
if (atdir == "up"){
nx = parseInt(atorig[0])
ny = parseInt(atorig[1]) - 1
atpos = nx + "." + ny
} else if (atdir == "down") {
nx = parseInt(atorig[0])
ny = parseInt(atorig[1]) + 1
atpos = nx + "." + ny
} else if (atdir == "left") {
nx = parseInt(atorig[0]) - 1
ny = parseInt(atorig[1])
atpos = nx + "." + ny
} else if (atdir == "right") {
nx = parseInt(atorig[0]) + 1
ny = parseInt(atorig[1])
atpos = nx + "." + ny
};
if (collmap[atpos] == 0) {
for (var bomb = coredata.bombs.length -1; bomb >= 0; bomb--){
if (coredata.bombs[bomb].pos == atpos){
return;
}
};
coredata.bombs.push({"pos": atpos, "state": "20", "owner": attacker});
console.log(attacker + " placed bomb");
};
};
function explode(bomb) {
var radius = 4;
dbomb = coredata.bombs[bomb];
posx = parseInt(dbomb.pos.split(".")[0]);
posy = parseInt(dbomb.pos.split(".")[1]);
console.log("boom @ " + dbomb.pos, posx, posy)
dodamage(dbomb.pos);
bombAffect = [];
//x
//left
for (var lx = posx -1; lx >= posx-radius; lx--){
var atpos = lx + "." + posy
if (collmap[atpos] !== 1){
if (collmap[atpos] > 1){
collmap[atpos] = 0;
mapchange = true;
break;
} else {
dodamage(atpos);
};
if (lx !== posx - 3){
coredata.effects.push("12" + "." + atpos);
} else {coredata.effects.push("13" + "." + atpos);};
} else {
break;
};
};
//right
for (var rx = posx + 1 ; rx <= posx + radius; rx++) {
atpos = rx + "." + posy
if (collmap[atpos] !== 1){
if (collmap[atpos] == 2){
collmap[atpos] = 0;
mapchange = true;
break;
} else {
dodamage(atpos);
};
if (rx !== posx + 3){
coredata.effects.push("12" + "." + atpos);
} else {coredata.effects.push("13" + "." + atpos);};
} else {
break;
};
};
//y
//up
for (var uy = posy -1; uy >= posy-radius; uy--){
var atpos = posx + "." + uy
if (collmap[atpos] !== 1){
if (collmap[atpos] == 2){
collmap[atpos] = 0;
mapchange = true;
break;
} else {
dodamage(atpos);
};
if (uy !== posy - 3){
coredata.effects.push("12" + "." + atpos);
} else {coredata.effects.push("13" + "." + atpos);};
} else {
break;
};
};
//down
for (var dy = posy + 1 ; dy <= posy + radius; dy++) {
atpos = posx + "." + dy
if (collmap[atpos] !== 1){
if (collmap[atpos] == 2){
collmap[atpos] = 0;
mapchange = true;
break;
} else {
dodamage(atpos);
};
if (dy !== posy + 3){
coredata.effects.push("12" + "." + atpos);
} else {coredata.effects.push("13" + "." + atpos);};
} else {
break;
};
};
};
function dodamage(atpos){
dp = coredata.players;
damage = 100;
for (var key in dp){
if (dp.hasOwnProperty(key)) {
if (dp[key].pos == atpos) {
dp[key].health = dp[key].health - damage;
if (dp[key].health <= 0){
dp[key].pos = dp[key].origin;
dp[key].health = 100;
console.log(dp[key], ' killed at ', atpos)
};
} ;
};
};
dp = coredata.npcs;
for (var key in dp){
if (dp.hasOwnProperty(key)) {
if (dp[key].pos == atpos){
dp[key].health = dp[key].health - damage;
if (dp[key].health <= 0){
dp[key].state = "dead";
console.log(dp[key], ' killed at ', atpos)
};
}
};
};
};