-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.js
126 lines (94 loc) · 3.7 KB
/
planet.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
var planet = {
p_colour: 999,
p_orbit_object: 0,
p_orbit_dist: 0,
p_name: "planet",
create: function(x, y, mass, width, colour, name){
var obj = Object.create(this);
obj.physical = particle.create("planet", x, y, mass, width, name);
obj.atmosphere = atmosphere.create(width, 3000);
obj.p_name = name;
obj.p_colour = colour;
return obj;
},
orbit: function(obj){
this.p_orbit_object = obj;
this.p_orbit_dist = calculate_distance(obj.physical.getx() , this.physical.getx(), obj.physical.gety(), this.physical.gety());
var velocity = (game.getgravity() * (obj.physical.getmass() + this.physical.getmass()));
velocity /= this.p_orbit_dist;
velocity = Math.sqrt(velocity * 0.008);
this.physical.setvelocity(-velocity, 0)
},
draw: function () {
this.atmosphere.draw(this.physical.getx(), this.physical.gety());
game.getcontext().fillStyle= '#' + this.p_colour;
game.getcontext().beginPath();
game.getcontext().arc(this.physical.getx() + game.screen.x, this.physical.gety() + game.screen.y, this.physical.getradius(), 0, 2 * Math.PI);
game.getcontext().fill();
},
update: function (time) {
this.keep_orbit();
this.physical.update(time);
return 0;
},
keep_orbit: function (){
if(this.p_orbit_object == 0){return;}
var dist = calculate_distance(this.physical.getx() , this.p_orbit_object.physical.getx(), this.physical.gety(), this.p_orbit_object.physical.gety());
// NEEDS TO WORK OUT ANGLE IT IS AND THEN THE DISTANCE FROM ITS PROPPER POSITION
var allowance = 0.01;
var power = 100000000;
if(dist < this.p_orbit_dist - (allowance * this.p_orbit_dist)){
var direction = Math.atan2(this.physical.getx() - this.p_orbit_object.physical.getx(), this.physical.gety() - this.p_orbit_object.physical.gety());
this.physical.addforce(power * Math.cos(direction) , power * Math.sin(direction));
}
if(dist > this.p_orbit_dist + (allowance * this.p_orbit_dist)){
var direction = Math.atan2(this.physical.getx() - this.p_orbit_object.physical.getx(), this.physical.gety() - this.p_orbit_object.physical.gety());
this.physical.addforce(-power * Math.cos(direction) , -power * Math.sin(direction));
}
},
collided: function(obj){
if(this.physical.collided(obj)){
if(obj === game.getplayer().physical){
game.getui().onplanet(this, true);
}
}
},
getname: function(){
return this.p_name;
}
};
var atmosphere = {
p_thickness: 0,
p_breathable: 0,
p_colour: 0,
p_weather: 0,
p_radius: 10,
p_atmospheresize: 0,
create: function(radius, atmospheresize){
radius += atmospheresize;
var obj = Object.create(this);
obj.p_atmospheresize = atmospheresize;
obj.physical = particle.create("atmosphere", 0, 0, 200, radius);
obj.p_radiusm = radius;
obj.physical.setdensity(0.01);
return obj;
},
draw: function(x, y){
this.physical.setx(x);
this.physical.sety(y);
var gradient = game.getcontext().createRadialGradient((this.physical.getx() + game.screen.x), (this.physical.gety() + game.screen.y), (this.physical.getradius() - this.p_atmospheresize), (this.physical.getx() + game.screen.x), (this.physical.gety() + game.screen.y), this.physical.getradius());
gradient.addColorStop(0,'rgba(200, 200, 255, 1)');
gradient.addColorStop(0.1,'rgba(200, 200, 255, 1)');
gradient.addColorStop(1,'rgba(0, 0, 0, 0.1)');
// Fill with gradient
game.getcontext().fillStyle = gradient;
//fix for firefox
if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
game.getcontext().fillStyle = 'rgba(200, 200, 200, 0.8)';
}
game.getcontext().beginPath();
game.getcontext().arc(this.physical.getx() + game.screen.x, this.physical.gety() + game.screen.y, this.physical.getradius(), 0, 2 * Math.PI);
game.getcontext().fill();
}
};