-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.js
82 lines (73 loc) · 3.4 KB
/
Ship.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
var shipCounter = 0;
function createShipElement()
{
var ship = svgDocument.createElementNS("http://www.w3.org/2000/svg", "g");
ship.setAttribute("id", "ship" + shipCounter);
var polygon = svgDocument.createElementNS("http://www.w3.org/2000/svg", "polygon");
polygon.setAttribute("id", "innership" + shipCounter);
polygon.setAttribute("points", "0,5 -5,10 0,-15 5,10");
polygon.setAttribute("style", "fill:none; stroke:white; stroke-width:2");
ship.appendChild(polygon);
var flame = svgDocument.createElementNS("http://www.w3.org/2000/svg", "polyline");
flame.setAttribute("id", "flame" + shipCounter);
flame.setAttribute("points", "-3,7 0,13 3,7");
flame.setAttribute("style", "stroke:white; stroke-width:1; visibility:hidden;");
ship.appendChild(flame);
svgRoot.appendChild(ship);
shipCounter++;
return ship;
}
function Ship(x, y)
{
this.ship = createShipElement();
this.polygon = this.ship.firstChild;
this.flame = this.ship.lastChild;
this.ghosts = [createShipElement(),createShipElement(),createShipElement(),createShipElement(),createShipElement(),createShipElement(),createShipElement(),createShipElement()];
this.live = true;
this.x = x;
this.y = y;
this.rot = 0;
this.vx = 0;
this.vy = 0;
}
Ship.prototype.explode = function()
{
this.live = false;
new Explosion(this.ship, 0, 0, 50, 20);
this.polygon.style.setProperty("visibility", "hidden");
this.flame.style.setProperty("visibility", "hidden");
}
Ship.prototype.updateShip = function()
{
if(this.live){
if(upArrow)
{
this.flame.style.visibility = 'visible';
for(var i = 0; i < this.ghosts.length; i++)
this.ghosts[i].lastChild.style.visibility = 'visible';
}
else
{
this.flame.style.visibility = 'hidden';
for(var i = 0; i < this.ghosts.length; i++)
this.ghosts[i].lastChild.style.visibility = 'hidden';
}
}
if(this.x < 0 && this.vx < 0) // off left edge
this.x += innerWidth;
if(this.x > innerWidth && this.vx > 0) // off right edge
this.x -= innerWidth;
if(this.y < 0 && this.vy < 0) // off top edge
this.y += innerHeight;
if(this.y > innerHeight && this.vy > 0) // off bottom edge
this.y -= innerHeight;
this.ship.setAttribute("transform", "translate(" + this.x + " " + this.y + ") rotate(" + this.rot + ")");
this.ghosts[0].setAttribute("transform", "translate(" + (this.x + innerWidth) + " " + this.y + ") rotate(" + this.rot + ")");
this.ghosts[1].setAttribute("transform", "translate(" + (this.x - innerWidth) + " " + this.y + ") rotate(" + this.rot + ")");
this.ghosts[2].setAttribute("transform", "translate(" + this.x + " " + (this.y + innerHeight) + ") rotate(" + this.rot + ")");
this.ghosts[3].setAttribute("transform", "translate(" + this.x + " " + (this.y - innerHeight) + ") rotate(" + this.rot + ")");
this.ghosts[4].setAttribute("transform", "translate(" + (this.x + innerWidth) + " " + (this.y + innerHeight) + ") rotate(" + this.rot + ")");
this.ghosts[5].setAttribute("transform", "translate(" + (this.x + innerWidth) + " " + (this.y - innerHeight) + ") rotate(" + this.rot + ")");
this.ghosts[6].setAttribute("transform", "translate(" + (this.x - innerWidth) + " " + (this.y + innerHeight) + ") rotate(" + this.rot + ")");
this.ghosts[7].setAttribute("transform", "translate(" + (this.x - innerWidth) + " " + (this.y - innerHeight) + ") rotate(" + this.rot + ")");
}