-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
83 lines (73 loc) · 2.21 KB
/
Player.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
RFall.Player = function () {
this.imgLeft = RFall.resources.images.playerLeft
this.imgRight = RFall.resources.images.playerRight
this.img = this.imgRight
this.hurting = false
this.x = 0
this.y = 0
this.width = 27
this.height = 56
this.speed = 15
this.resetPosition()
}
RFall.Player.prototype.takeDamage = function () {
RFall.player.removeHeart()
RFall.player.setHurting( true )
if (!this.isDead())
setTimeout("RFall.player.setHurting( false )",1500)
}
RFall.Player.prototype.isHurting = function () {
return this.hurting
}
RFall.Player.prototype.setHurting = function ( bool ) {
var currentlyLeft = (this.img == this.imgLeft)
if (bool == false) {
this.imgLeft = RFall.resources.images.playerLeft
this.imgRight = RFall.resources.images.playerRight
this.hurting = false
}
if (bool == true) {
this.imgLeft = RFall.resources.images.playerHurtLeft
this.imgRight = RFall.resources.images.playerHurtRight
this.hurting = true
}
this.img = (currentlyLeft) ? this.imgLeft : this.imgRight
RFall.draw()
}
RFall.Player.prototype.move = function ( direction ) {
if (direction == "left") {
this.img = this.imgLeft
this.x -= this.speed
if (this.x < RFall.cage.cageLBound)
this.x = RFall.cage.cageLBound
RFall.draw()
}
if (direction == "right") {
this.img = this.imgRight
this.x += this.speed
if (this.x > RFall.cage.cageRBound - this.width)
this.x = RFall.cage.cageRBound - this.width
RFall.draw()
}
}
RFall.Player.prototype.resetPosition = function () {
this.x = Math.floor( RFall.randomMinMax(RFall.cage.cageLBound, RFall.cage.cageRBound - this.width) )
this.y = RFall.canvasHeight - RFall.cage.cageFloorHeight - this.height
}
// draw( ctx )
// ctx is a 2D canvas context
RFall.Player.prototype.draw = function (ctx) {
ctx.drawImage(RFall.player.img, RFall.player.x, RFall.player.y, this.width, this.height)
for (var h = 1; h <= RFall.hearts; h++){
ctx.drawImage(RFall.resources.images.heart, RFall.canvasWidth - (40 * h), 0, 40, 40)
}
}
RFall.Player.prototype.removeHeart = function () {
--RFall.hearts
}
RFall.Player.prototype.isDead = function () {
if (RFall.hearts < 0)
return true
else
return false
}