-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.js
139 lines (120 loc) · 3.9 KB
/
character.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
function Character() {
Collidable.call(this);
this.speed = {
x: 0,
y: 0,
value: 0
}
this.dir = DIR.N;
this.health = 1;
this.touched = function() {
this.health--;
if (this.health <= 0) {
this.state = STATE.DEAD;
}
}
this.stop = function() {
this.speed.x = 0;
this.speed.y = 0;
}
// PathFinder
this.updateDir = function() {}
// Move according to pathFider
this.updatePos = function() {
var xStart = this.x;
var yStart = this.y;
if (this.state != STATE.MOVE && this.state != STATE.INVINCIBLE || player.state == STATE.DEAD) {
return;
}
switch (this.dir) {
case DIR.N:
this.y -= this.speed;
break;
case DIR.S:
this.y += this.speed;
break;
case DIR.W:
this.x -= this.speed;
break;
case DIR.E:
this.x += this.speed;
break;
default:
break;
}
this.x = constrain(this.x, 0, width);
this.y = constrain(this.y, 0, height);
if (this.speed > 10) {
stroke(255, 200);
line(this.x, this.y, xStart, yStart);
line(this.x - 10, this.y, xStart - 10, yStart);
line(this.x + 10, this.y, xStart + 10, yStart);
line(this.x, this.y - 10, xStart, yStart - 10);
line(this.x, this.y + 10, xStart, yStart + 10);
}
}
this.update = function() {
this.updateDir();
this.updatePos();
if (this.stamina != undefined) {
this.stamina = constrain(player.stamina + 1, 0, 150);
}
}
this.draw = function() {
push();
// Position
translate(this.x, this.y);
switch (this.dir) {
case DIR.N:
break;
case DIR.S:
rotate(PI);
break;
case DIR.W:
rotate(3 * PI / 2);
break;
case DIR.E:
rotate(PI / 2);
break;
default:
break;
}
// Display
this.setSprites(this.getSpriteName(), this.nbSprites);
img = this.sprites[(floor(frameCount / 5) + this.spriteOffset) % this.nbSprites];
imageMode(CENTER);
if (this.tint.R + this.tint.G + this.tint.B != 3 * 255) {
tint(this.tint.R, this.tint.G, this.tint.B);
}
image(img, 0, 0, img.width / this.scl, img.height / this.scl);
noTint();
pop();
if (this.health > 1 && this.health < 10) {
var heart = getAssetManager().getSprites("infos.heart", 1)[0];
var heartScale = 15;
for (var i = 0; i < this.health; i++) {
image(heart,
this.x - Math.floor(this.health / 2 - i) * heart.width / heartScale,
this.y - 1.5 * this.size,
heart.width / heartScale,
heart.height / heartScale);
// image(heart, this.x - (this.health / 2 + i) * heart.width / 10, -img.height, heart.width / 10, heart.height / 10);
}
}
if (this.stamina != undefined) {
if (this.stamina > 100) {
fill(255, 170, 0, 200);
} else {
fill(255, 0, 0, 200);
}
rectMode(CENTER);
rect(this.x, this.y - 35, this.stamina / 1.5, 5);
if (this.stamina > 100) {
strokeWeight(2);
line(this.x + (this.stamina - 100) / 3, this.y - 37, this.x + (this.stamina - 100) / 3, this.y - 33);
line(this.x - (this.stamina - 100) / 3, this.y - 37, this.x - (this.stamina - 100) / 3, this.y - 33);
strokeWeight(1);
}
}
}
}