-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
127 lines (107 loc) · 3.71 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
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
import {Sitting,Running,Jumping,Falling,Rolling,Diving,Hit} from "./playerstates.js";
import { collosionAnimation } from "./collisionAnimation.js";
import { FloatingMsg } from "./floatingMsg.js";
export class Player{
constructor(game){
this.game=game;
this.width=100;
this.height=91.3;
this.x=0;
this.y=game.height-this.height-this.game.groundMargin;
this.vy=0;
this.weight=1;
this.image=new Image();
this.image.src='/assets/spriteSheet.png';
this.frameX=0;
this.frameY=0;
this.maxFrame;
this.fps=30;
this.frameInterval=1000/this.fps;
this.frameTimer=0;
this.speed=0;
this.maxSpeed=10;
this.states=[new Sitting(this.game),new Running(this.game),new Jumping(this.game),new Falling(this.game),new Rolling(this.game),new Diving(this.game),new Hit(this.game)];
this.currentState=null;
}
update(input,deltaTime)
{
this.checkCollisions();
this.currentState.handleInput(input);
// horizontal movements
this.x +=this.speed;
if(input.includes('ArrowRight')) this.speed=this.maxSpeed;
else if(input.includes('ArrowLeft')) this.speed= -this.maxSpeed;
else this.speed=0;
if(this.x<0)
{
this.x=0;
if(input.lenght!=0)
{
input.splice(0,input.length);
}
}
if(this.x>this.game.width-this.width)
{
this.x= this.game.width-this.width;
if(input.lenght!=0)
{
input.splice(0,input.length);
}
}
// vertical movements
this.y+=this.vy;
if(!this.onGround())this.vy+=this.weight;
else this.vy=0;
if(this.y>this.game.height-this.game.groundMargin-this.height)
{
this.y=this.game.height-this.height-this.game.groundMargin;
}
//sprite Animations
if(this.frameTimer>this.frameInterval)
{
this.frameTimer=0;
if(this.frameX<this.maxFrame)this.frameX++;
else this.frameX=0;
}
else
{
this.frameTimer+=deltaTime;
}
}
draw(context){
if(this.game.debug) context.strokeRect(this.x,this.y,this.width,this.height);
context.drawImage(this.image,this.frameX*this.width,this.frameY*this.height,this.width,this.height,this.x,this.y,this.width,this.height);
}
onGround()
{
return this.y>=this.game.height-this.height-this.game.groundMargin;
}
setState(state,speed)
{
this.currentState=this.states[state];
this.game.speed=this.game.maxSpeed*speed;
this.currentState.enter();
}
checkCollisions(){
this.game.enemies.forEach(enemy => {
if(
enemy.x<this.x+ this.width &&
enemy.x +enemy.width >this.x &&
enemy.y<this.y +this.height &&
enemy.y+enemy.height>this.y
){
enemy.markedForDeletion=true;
this.game.collisions.push(new collosionAnimation(this.game,enemy.x+enemy.width*0.5,enemy.y+enemy.height*0.5));
if(this.currentState==this.states[4] || this.currentState==this.states[5]){
this.game.score++;
this.game.floatingMessages.push(new FloatingMsg('+1',enemy.x,enemy.y,150,50));
}else{
this.setState(6,0);
this.game.score-=5;
this.game.lives--;
if(this.game.lives<=0) this.game.gameOver=true;
}
}
});
}
}