-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateFleet.js
64 lines (60 loc) · 1.84 KB
/
generateFleet.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
function Ship(length,type) {
this.length = length;
this.tiles = [];
this.type = type;
this.owner;
};
Ship.prototype.placeShip =
function (tile, direction,owner) {
switch (direction) {
case "horizontal":
for (let i = 0; i < this.length; i++) {
let mapTile = owner.tileMap[tile.X][tile.Y + i];
mapTile.classList.add(this.type + (i+1));
mapTile.classList.add("horizontal");
mapTile.taken = true;
mapTile.ship = this;
this.tiles.push(mapTile);
}
break;
case "vertical":
for (let i = 0; i < this.length; i++) {
let mapTile = owner.tileMap[tile.X + i][tile.Y];
mapTile.classList.add(this.type + (i+1));
mapTile.classList.add("vertical");
mapTile.taken = true;
mapTile.ship = this;
this.tiles.push(mapTile);
}
break;
}
this.direction = direction;
this.owner = owner;
}
Ship.prototype.takeDamage =
function(tile) {
this.tiles.pop();
if(this.tiles.length === 0)
{
this.owner.ships.pop();
}
tile.style.classList = "tile-hit";
}
function Battleship() {
Ship.call(this, 5,"battleship");
};
Battleship.prototype = Object.create(Ship.prototype);
Object.defineProperty(Battleship.prototype, "constructor", {
value: Battleship,
enumerable: false,
writable: true
});
function Destroyer() {
Ship.call(this, 4,"destroyer");
};
Destroyer.prototype = Object.create(Ship.prototype);
Object.defineProperty(Destroyer.prototype, "constructor", {
value: Destroyer,
enumerable: false,
writable: true
});