-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNatureDecoration.js
105 lines (84 loc) · 3.72 KB
/
NatureDecoration.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
function NatureDecoration(type, index, position, fruitType) {
powerupjs.AnimatedGameObject.call(this, 1, ID.layer_background);
this.index = index;
this.spriteType = type;
this.type = "nature";
this.loadAnimation(sprites.nature[type].idle, "idle", false);
this.loadAnimation(sprites.nature[type].shake, "shake", false, 0.09);
this.fruitType = fruitType;
console.log(fruitType)
this.fruits = new powerupjs.GameObjectList(ID.layer_background_1)
if (this.fruitType !== undefined) {
var fruit1 = new powerupjs.SpriteGameObject(sprites.fruits[fruitType], 1, 0, ID.layer_overlays);
fruit1.position = new powerupjs.Vector2(position.x + 20, position.y + 20);
this.fruits.add(fruit1)
var fruit2 = new powerupjs.SpriteGameObject(sprites.fruits[fruitType], 1, 0, ID.layer_overlays);
fruit2.position = new powerupjs.Vector2(position.x, position.y + 10);
this.fruits.add(fruit2)
var fruit3 = new powerupjs.SpriteGameObject(sprites.fruits[fruitType], 1, 0, ID.layer_overlays);
fruit3.position = new powerupjs.Vector2(position.x + 40, position.y + 10);
this.fruits.add(fruit3)
}
this.playAnimation("idle");
console.log(this.fruits)
}
NatureDecoration.prototype = Object.create(
powerupjs.AnimatedGameObject.prototype
);
NatureDecoration.prototype.update = function (delta) {
powerupjs.AnimatedGameObject.prototype.update.call(this, delta);
var feild = this.parent;
var player = feild.find(ID.player);
var switchPoint = this.position.y // Where the player changes layer
if (this.spriteType === 'oak_tree' || this.spriteType === 'pine_tree') {
switchPoint = this.position.y + 40 // only if its a tree
}for (var i=0; i<feild.gameObjects.length; i++) {
if (feild.gameObjects[i].animated) {
if (feild.gameObjects[i].position.y > switchPoint) {
this.layer = ID.layer_background;
} else {
this.layer = ID.layer_background_2;
}
feild.gameObjects.sort(function (a, b) {
return a.layer - b.layer;
});
}
}
this.fruits.update(delta)
var distanceX = Math.abs(player.position.x - this.position.x);
var distanceY = Math.abs(player.position.y - this.position.y);
var distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY)
if (distance < 60) { // Is the player close?
if (powerupjs.Keyboard.pressed(32)) {
console.log(this.spriteType)
if (this.spriteType === 'oak_tree' || this.spriteType === 'pine_tree')
this.shake()
}
}
else {
this.playAnimation('idle') // Change back if player walks away
}
};
NatureDecoration.prototype.shake = function() {
this.playAnimation('shake');
var area = powerupjs.Game.gameWorld.map.areas[powerupjs.Game.gameWorld.map.currentAreaIndex];
var objects = area.find(ID.objects);
console.log(objects)
if (this.fruitType !== undefined && this.fruits.visible) {
console.log(this.fruitType)
var droppedFruit1 = new Plant(this.fruitType, 'tree_fruit', new powerupjs.Vector2(0, this.position.y + 80));
droppedFruit1.position = new powerupjs.Vector2(this.position.x + 20, this.position.y + 20)
objects.add(droppedFruit1)
var droppedFruit2 = new Plant(this.fruitType, 'tree_fruit', new powerupjs.Vector2(0, this.position.y + 60));
droppedFruit2.position = new powerupjs.Vector2(this.position.x, this.position.y + 10)
objects.add(droppedFruit2)
var droppedFruit3 = new Plant(this.fruitType, 'tree_fruit', new powerupjs.Vector2(0, this.position.y + 60));
droppedFruit3.position = new powerupjs.Vector2(this.position.x + 40, this.position.y + 10)
objects.add(droppedFruit3)
this.fruits.visible = false // Hide the fruits in the tree
}
}
NatureDecoration.prototype.draw = function() {
powerupjs.AnimatedGameObject.prototype.draw.call(this);
this.fruits.draw()
}