A collection of games I worked on in high school.
1var supertotalenemies = []; //all enemies in the game currently //returned in message
2var response = []; //returned in message
3response[0] = []; //dead
4response[1] = []; //moved
5response[2] = []; //health
6var deltax = 0;
7var deltay = 0;
8var message = {}; //object coming from app.js
9var da_zone = 0; //area in which unit should stop moving, if melee it's much closer
10
11var WINDOW_WIDTH = 800;
12var WINDOW_HEIGHT = 600;
13var PLAYER_POSX = WINDOW_WIDTH / 2 - 48;
14var PLAYER_POSY = WINDOW_HEIGHT / 2 - 48;
15var PAWN_SPEED = 9;
16
17//enemy damage
18var MELEE_PAWN = .2; //damage for melee units
19var MELEE_BISHOP = 1;
20var MELEE_KNIGHT = 1;
21var MELEE_ROOK = 1;
22var MELEE_KING = 1;
23var MELEE_QUEEN = 4;
24var RANGED_PAWN = .5; //damage for ranged units
25var RANGED_BISHOP = 2.5;
26var RANGED_KNIGHT = 2.5;
27var RANGED_ROOK = 2.5;
28var RANGED_KING = 2.5;
29var RANGED_QUEEN = 10;
30var damage = { "w": { "pawn": MELEE_PAWN, "bishop": MELEE_BISHOP, "knight": MELEE_KNIGHT, "rook": MELEE_ROOK, "king": MELEE_KING, "queen": MELEE_QUEEN }, "b": { "pawn": RANGED_PAWN, "bishop": RANGED_BISHOP, "knight": RANGED_KNIGHT, "rook": RANGED_ROOK, "king": RANGED_KING, "queen": RANGED_QUEEN } }; //is set to melee or ranged based off of unit type when looping through
31
32var first = true;
33
34importScripts('workerFunctions.js');
35
36onmessage = function (event) { //this function looks for data recieved and appends it to supertotalenemies or removes it if they're dead
37 if (first) {
38 debugger;
39 first = false;
40 }
41
42 message = JSON.parse(event.data); //turns the text back into an object
43 if (message.newenemies.length > 0) {
44 supertotalenemies.push.apply(supertotalenemies, message.newenemies); //adds new enemies to the supertotalenemies array
45 }
46 if (message.hitenemies.length > 0) {
47 for (var k = 0; k <= message.hitenemies.length - 1; k++) {
48 supertotalenemies.cleanse(message.hitenemies[k].id);
49 response[0].push(message.hitenemies[k].id);
50 }
51 }
52 deltax = message.delta.x;
53 deltay = message.delta.y;
54
55 for (var j = 0; j <= supertotalenemies.length - 1; j++) {
56 //checks type of unit to change the stop range and damage value
57 if (supertotalenemies[j].color == "w") {
58 da_zone = 64; //64 px from player
59 }
60 else {
61 da_zone = 160; //160 px from player
62 }
63
64 //outofbounds - 32 past farthest spawn distance
65 if (supertotalenemies[j].x < -288 ||
66 supertotalenemies[j].x > WINDOW_WIDTH + 192 ||
67 supertotalenemies[j].y < -288 ||
68 supertotalenemies[j].y > WINDOW_HEIGHT + 192) {
69
70 response[0].push(supertotalenemies[j].id); //adds the name of the element to be removed to the response array
71 }
72 //enemy movement
73 else {
74 var direction_vector = normal({ x: PLAYER_POSX - supertotalenemies[j].x, y: PLAYER_POSY - supertotalenemies[j].y });
75 var x = deltax;
76 var y = deltay;
77
78 //check to see already hit da zone of attacking
79 //the following blocks make enemies move until they're within a certain range of the player at which point they stop for bit (play attack anim?) and then move again
80 if (supertotalenemies[j].touching === true && supertotalenemies[j].touchingcount < supertotalenemies[j].touchingmax) {
81 supertotalenemies[j].touchingcount++;
82 if (supertotalenemies[j].touchingcount == 1) {
83 response[2].push(damage[supertotalenemies[j].color][supertotalenemies[j].type]); //adds amount of damage done to the response array
84 }
85 supertotalenemies[j].x += x;
86 supertotalenemies[j].y += y;
87 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
88 }
89 else {
90 supertotalenemies[j].touchingcount = 0;
91
92 //check to see if entered the da zone of attacking
93 if ((((PLAYER_POSX - da_zone) < supertotalenemies[j].x) && (supertotalenemies[j].x < (PLAYER_POSX + da_zone))) &&
94 (((PLAYER_POSY - da_zone) < supertotalenemies[j].y) && (supertotalenemies[j].y < (PLAYER_POSY + da_zone)))) {
95 supertotalenemies[j].touching = true;
96 supertotalenemies[j].x += x;
97 supertotalenemies[j].y += y;
98 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
99 }
100 else {
101
102 //handle movement
103 x += PAWN_SPEED * direction_vector.x;
104 y += PAWN_SPEED * direction_vector.y;
105
106 supertotalenemies[j].touching = false;
107
108 switch (supertotalenemies[j].type) {
109 case "pawn":
110 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y }); //adds the id, x, and y coordinates that the enemy moved to to the response array
111 supertotalenemies[j].x += x;
112 supertotalenemies[j].y += y;
113 break;
114 case "knight":
115 //does the funky movement stuff
116 if (supertotalenemies[j].temp < 30) {
117 //makes it so that if the piece is within attacking range for x only y is calculated
118 if (PLAYER_POSX - supertotalenemies[j].x < da_zone && PLAYER_POSX - supertotalenemies[j].x > da_zone * -1) {
119 supertotalenemies[j].temp = 31;
120 }
121 else {
122 x = deltax;
123 supertotalenemies[j].temp++;
124 supertotalenemies[j].x -= x;
125 supertotalenemies[j].y += y;
126 }
127 }
128 else {
129 if (supertotalenemies.temp < 60) {
130 //makes it so that if the peice is within attacking range for y only x is calculated
131 if (PLAYER_POSY - supertotalenemies[j].y < da_zone && PLAYER_POSY - supertotalenemies[j].y > da_zone * -1) {
132 supertotalenemies[j].temp = 0;
133 }
134 else {
135 y = deltay;
136 supertotalenemies[j].temp++;
137 supertotalenemies[j].x += x;
138 supertotalenemies[j].y -= y;
139 }
140 }
141 else { //should only run the first time to make temp a thing
142 supertotalenemies[j].temp = 0;
143 y = deltay;
144 supertotalenemies[j].x += x;
145 supertotalenemies[j].y += y;
146 }
147 }
148 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
149 break;
150 case "bishop":
151 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
152 supertotalenemies[j].x += x;
153 supertotalenemies[j].y += y;
154 break;
155 case "rook":
156 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
157 supertotalenemies[j].x += x;
158 supertotalenemies[j].y += y;
159 break;
160 case "queen":
161 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
162 supertotalenemies[j].x += x;
163 supertotalenemies[j].y += y;
164 break;
165 case "king":
166 response[1].push({ "id": supertotalenemies[j].id, "x": x, "y": y });
167 supertotalenemies[j].x += x;
168 supertotalenemies[j].y += y;
169 break;
170 }
171 }
172 }
173 }
174 }
175 if (response[0].length !== 0 || response[1].length !== 0 || response[2].length !== 0) {
176 postMessage(JSON.stringify(response)); //sends the response array back to app.js as text
177
178 if (response[0].length > 0) {
179 supertotalenemies.removegiven("id", response[0]);//custom function that removes the given array or string from an array
180 }
181
182 response[0].length = 0; //clears the removed enemies if app.js responded (meaning that they have been removed from the game)
183 response[1].length = 0; //clears moved enemies
184 response[2].length = 0; //clears health changes
185 }
186};