-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerEntity.js
245 lines (205 loc) · 5.7 KB
/
playerEntity.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*-------------------
a player entity
-------------------- */
var playerEntity = me.ObjectEntity.extend({
/* -----
constructor
------ */
init: function(x, y, settings) {
// settings
settings.type = "player";
settings.image = "avatar";
settings.spritewidth = 16;
settings.spriteheight = 16;
// this.transparent_color="ffffff";
// call the constructor
this.parent(x, y, settings);
this.setVelocity(2, 2);
this.gravity=0;
this.direction = "left";
this.moving = false;
this.stance = "normal";
this.actionActive = false;
this.collidable=true;
//stats
this.stage = "alive";
this.hp = 100;
this.mp = 40;
this.str = 120;
this.itl = 40;
this.dex = 120
this.damage = 0;
this.hci = 0;
this.swingspeed = 0;
//weapon
this.swing = false;
this.weapon = {};
this.equipweapon = false;
me.debug.renderHitBox = false;
// head info
//this.hpValue = new me.Font('font8', 8, 'white');
//animation
this.addAnimation("normal_up",[0,1,2]);
this.addAnimation("normal_right",[4,5,6]);
this.addAnimation("normal_down",[8,9,10]);
this.addAnimation("normal_left",[12,13,14]);
this.setCurrentAnimation(this.stance + "_" + this.direction);
// adjust the bounding box x,w,y,h
this.updateColRect(5,10 , 5, 10);
// set the display to follow our position on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
},
/* -----
update the player pos
------ */
update: function() {
//
//
// Handle Inputs
//
//
if (me.input.isKeyPressed('left')) {
this.doWalk("left");
} else if (me.input.isKeyPressed('right')) {
this.doWalk("right");
} else if (me.input.isKeyPressed('up')) {
this.doWalk("up");
} else if (me.input.isKeyPressed('down')) {
this.doWalk("down");
} else {
this.doStand();
};
if (me.input.isKeyPressed('action')) {
this.actionActive = true;
} else {
this.actionActive = false;
}
if (me.input.isKeyPressed('interact')) {
//var newSpider = me.entityPool.newInstanceOf("spider", this.x + 16, this.y)
//me.game.add(newSpider, this.z);
//me.game.sort();
}
//
//
// check collision
//
//
this.swing = true;
res = me.game.collide(this);
if (res)
{
// weapon collition
if (res.obj.type == "weapon") {
if (this.actionActive == true) {
this.swing = false;
// unequip
if(this.equipweapon){
this.weapon.obj.doUnEquip();
this.equipweapon = false;
};
// equip
this.weapon = res;
res.obj.doEquip(this.GUID);
this.equipweapon=true;
};
};
// mob collition
if (res.obj.type == "mob") {
this.swing = true;
res.obj.OnCollide(res,this);
}
// furniture collition
else if (res.obj.type == "furniture") {
this.swing = true;
// What to do if Collides
res.obj.OnCollide(res,this);
}
// container collition
else if (res.obj.type == "container"){
this.swing = false;
// What do do if interacts
if (this.actionActive == true) {
res.obj.OnInteract(this);
};
// What to do if Collides
res.obj.OnCollide(res,this);
}
// npc
else if (res.obj.type == "npc") {
this.swing = false;
}
// portal
else if (res.obj.type == "portal"){
this.swing = false;
// What do do if interacts
if (this.actionActive == true) {
res.obj.OnInteract(this);
};
// What to do if Collides
res.obj.OnCollide(res,this);
}
else {
this.swing = true;
};
};
// update player movement
if (this.moving == true) {
this.updateMovement();
this.parent(this);
return true;
} else {
return false
}
// check next level
// me.state.current().nextLevel();
},
draw: function (context)
{
//context.drawImage(me.loader.getImage("bar100"), this.pos.x - me.game.viewport.pos.x, this.pos.y - me.game.viewport.pos.y-(6+1));
//this.hpValue.draw(context, this.hp, this.pos.x - me.game.viewport.pos.x, this.pos.y - me.game.viewport.pos.y);
this.parent(context);
},
doWalk: function(newDirection){
this.moving = true;
this.direction=newDirection;
if (this.direction == "left") {
this.vel.x += -this.accel.x * me.timer.tick
} else if (this.direction == "right") {
this.vel.x += this.accel.x * me.timer.tick
} else if (this.direction == "up") {
this.vel.y += -this.accel.y * me.timer.tick
} else if (this.direction == "down") {
this.vel.y += this.accel.y * me.timer.tick
}
this.setCurrentAnimation(this.stance + "_" + this.direction)
},
doStand : function(up) {
this.moving = false;
this.vel.y = 0;
this.vel.x = 0;
},
// Receive Damage
doDamage: function(attacker,hci,damage) {
// calculate hc and do damage;
this.hp -= damage;
me.game.HUD.setItemValue("hp", this.hp);
// little bounce
if (attacker.direction == "left") {
this.pos.x-=10
};
if (attacker.direction == "right") {
this.pos.x+=10
};
if (attacker.direction == "up") {
this.pos.y-=10
};
if (attacker.direction == "down") {
this.pos.y+=10
};
// check if die
if (this.hp<=0){
this.stage = "dead"
me.game.remove(this);
}
}
});