-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstructors.js
113 lines (90 loc) · 2.53 KB
/
constructors.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
Game = {};
Game.DIM_X = 600;
Game.DIM_Y = 600;
// INHERITANCE
Function.prototype.inherits = function(SuperClass) {
function Surrogate() {};
Surrogate.prototype = SuperClass.prototype;
this.prototype = new Surrogate();
};
// MOVING OBJECT
MovingObject = MovingObject = function(pos, vel, radius, color, game_id) {
this.pos = pos;
this.vel = vel;
this.radius = radius;
this.color = color;
this.game_id = game_id;
};
// ASTEROID
Asteroid = function(pos, vel, radius, game_id) {
MovingObject.call(this,
pos,
vel,
radius,
Asteroid.randomColor(),
game_id
);
};
Asteroid.randomColor = function() {
var random = Math.random();
if (random <= 0.5) {
return "yellow";
} else {
return "black";
};
};
Asteroid.RADIUS_BOUND = 40;
Asteroid.inherits(MovingObject);
Asteroid.randomAsteroid = function(game_id) {
var radius = Asteroid.RADIUS_BOUND * (Math.random() + 0.2);
return new Asteroid(this.randomPos(radius), this.randomVel(), radius, game_id);
};
Asteroid.randomPos = function(radius) {
var posX = (Math.random() * (Game.DIM_X - 2 * radius) + radius);
var posY = (Math.random() * (Game.DIM_Y - 2 * radius) + radius);
var dist = function(x, y) {
var dx = (x - Game.DIM_X / 2);
var dy = (y - Game.DIM_Y / 2);
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
}
while (dist(posX, posY) < 200) {
posX = (Math.random() * (Game.DIM_X - 2 * radius) + radius);
posY = (Math.random() * (Game.DIM_Y - 2 * radius) + radius);
}
return [posX, posY];
};
Asteroid.randomVel = function() {
var dirX = ((Math.random() * 2) - 1);
var dirY = ((Math.random() * 2) - 1);
var speedX = Game.DIM_X / 70 * Math.random();
var speedY = Game.DIM_Y / 70 * Math.random();
return [dirX * speedX, dirY * speedY];
};
// SHIP
Ship = function(player_id, game_id) {
this.pos = Ship.spawnSpot(player_id);
this.angle = Math.PI;
this.radius = 25;
this.speed = 0;
this.color = Ship.color(player_id, game_id);
this.game_id = game_id;
this.player_id = player_id;
};
Ship.spawnSpot = function(player_id) {
var player = Players.findOne(player_id);
if (player.position === "first") {
return [Game.DIM_X / 3, Game.DIM_Y / 2];
} else if (player.position === "second") {
return [Game.DIM_X * 2 / 3, Game.DIM_Y / 2];
} else {
return [Game.DIM_X / 2, Game.DIM_X / 2];
}
}
Ship.color = function(player_id, game_id) {
var game = Games.findOne(game_id);
if (game.type === "single" || game.player1_id === player_id) {
return "blue"
} else {
return "red"
}
};