-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathItem.js
152 lines (129 loc) · 4.29 KB
/
Item.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(function() {
// Default item names
var ItemNames = {
HEALTHPACK: "healthpack",
DAMAGEBOOST: "damageboost",
SPEEDBOOST: "speedboost"
};
function Item(props) {
// Kontra init
this.init(props);
this.name = this.name || "";
// Set width and height - defaults to 20 X 20
this.width = this.width || 20;
this.height = this.height || 20;
// Set pickup radius
this.pickupRadius = this.width*2;
this.render = function(xView, yView) {
// Save context
this.context.save();
// Items are never fixed so update by offsets
var x = this.x - xView;
var y = this.y - yView;
// Switch the render based on the item's name
switch(this.name) {
case ItemNames.HEALTHPACK:
// Background
this.context.beginPath();
this.context.fillStyle = "white";
this.context.rect(x, y, this.width, this.height);
this.context.fill();
this.context.closePath();
// Cross
this.context.beginPath();
this.context.fillStyle = "red";
this.context.rect(x, y+(this.height/2)-(this.height)/8, this.width, this.height/4);
this.context.rect(x+(this.width/2)-(this.width/8), y, this.width/4, this.height);
this.context.fill();
this.context.closePath();
break;
case ItemNames.DAMAGEBOOST:
// Background
this.context.beginPath();
this.context.fillStyle = "lightgrey";
this.context.rect(x, y, this.width, this.height);
this.context.fill();
this.context.closePath();
// Up Arrows
this.context.beginPath();
this.context.fillStyle = "red";
// Top Arrow
this.context.moveTo(x+this.width/2, y+this.height/4);
this.context.lineTo(x+this.width/4, y+this.height/2);
this.context.lineTo(x+(this.width/1.3), y+this.height/2);
this.context.fill();
// Bottom Arrow
this.context.moveTo(x+this.width/2, y+this.height/2);
this.context.lineTo(x+this.width/4, y+this.height/1.25);
this.context.lineTo(x+(this.width/1.3), y+this.height/1.25);
this.context.fill();
this.context.closePath();
break;
case ItemNames.SPEEDBOOST:
// Background
this.context.beginPath();
this.context.fillStyle = "lightgrey";
this.context.rect(x, y, this.width, this.height);
this.context.fill();
this.context.closePath();
// Arrows
this.context.beginPath();
this.context.fillStyle = "limegreen";
// Left Arrow
this.context.moveTo(x, y);
this.context.lineTo(x+this.width/2, y+this.height/2);
this.context.lineTo(x, y+this.height);
// Right Arrow
this.context.moveTo(x+this.width/2, y);
this.context.lineTo(x+this.width, y+this.height/2);
this.context.lineTo(x+this.width/2, y+this.height);
this.context.fill();
this.context.closePath();
break;
default:
return;
}
this.context.restore();
}
}
// Inheritance
Item.prototype = Object.create(kontra.sprite.prototype);
Item.prototype.constructor = Item;
// Apply the affect to the given target
Item.prototype.applyEffect = function(target) {
switch(this.name) {
case ItemNames.HEALTHPACK:
var diff = target.maxHealth - target.health;
// If the diff is less than 10 add by that - no one gets MORE that full HEALTH
if(diff < 25) {
target.health += diff;
} else {
target.health += 25;
}
break;
case ItemNames.DAMAGEBOOST:
target.equippedWeapon.damage += 5;
break;
case ItemNames.SPEEDBOOST:
target.maxSpeed += 0.1;
break;
default:
return;
}
};
// Class function that returns a randomized item
Item.random = function() {
var rand = Game.Math.random();
var itemName;
if(rand < 0.15) {
itemName = ItemNames.DAMAGEBOOST;
} else if(rand > 0.25 && rand < 0.85) {
itemName = ItemNames.HEALTHPACK;
} else {
itemName = ItemNames.SPEEDBOOST;
}
return new Item({name: itemName});
}
// Add Item to Game
Game.Item = Item;
})();