-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
132 lines (118 loc) · 4.28 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
128
129
130
131
132
import { myInput } from "./myInput.js";
export class Player extends Phaser.Physics.Arcade.Sprite{
constructor(scene, x, y, texture){
super(scene, x, y, texture);
scene.physics.add.existing(this);
scene.add.existing(this);
this.scene = scene;
this.initProperties()
this.scene.events.on("update", this.update, this);
}
initProperties(){
this.graphics = this.scene.add.graphics()
.setScrollFactor(1)
.setDepth(209);
this.scene.add.existing(this.graphics);
this.isOnSlope = false;
//CREATE Player
this.speedX = 75;
this.speedY = 400;
this.bottomTile;
this.gravity = 982;
this.body.gravity.y = this.gravity;
this.jumping;
this
.setDepth(200)
.setCollideWorldBounds(true)
.setSize(15, 32)
}
jump(){
this.body.setAllowGravity(true);
this.body.setGravityY(this.gravity);
this.setVelocityY(-this.speedY);
}
handleMovement(){
//movement
if(myInput.keys[0] === "left"){
this.play("run", true);
this.setVelocityX(-this.speedX);
this.setFlipX(true);
}
else if(myInput.keys[0] === "right"){
this.play("run", true);
this.setVelocityX(this.speedX);
this.setFlipX(false);
}
else{
this.play("idle");
this.body.setVelocityX(0);
}
if(this.body.onFloor() && myInput.keys[0]==="up"){
this.play("jump", true);
this.jump();
}
}
handleInterSection(){
const { left_to_right_slopes, left_to_right_boxes, right_to_left_slopes, right_to_left_boxes } = this.scene.slopes;
//left slopes
left_to_right_boxes.forEach(box=>{
if(this.scene.physics.world.intersects(this.body, box.body)){
left_to_right_slopes.forEach(slope=>{
if(this.intersects(this, slope)){
this.isOnSlope = true;
if(myInput.keys[0]=== "up"){
this.jump();
}
else{
this.isOnSlope = false;
const dX = this.body.right - box.body.left;
this.body.position.y = box.body.bottom - this.height - dX;
if(myInput.keys[0] !== "right" ){
this.body.position.x -= 0.6; //auto slide
this.setFlipX(true);
}
this.body.setAllowGravity(false);
}
}
})
}
else{
this.body.setAllowGravity(true);
}
})
//right slopes
right_to_left_boxes.forEach(box=>{
if(this.scene.physics.world.intersects(this.body, box.body)){
right_to_left_slopes.forEach(slope=>{
if(this.intersects(this, slope)){
this.isOnSlope = true;
if(myInput.keys[0]=== "up"){
this.jump();
}
else{
this.isOnSlope = false;
const dX = box.body.right - this.body.left;
this.body.position.y = box.body.bottom - this.height - dX;
if(myInput.keys[0] !== "left" ){
this.body.position.x += 0.6; //auto slide
this.setFlipX(false);
}
this.body.setAllowGravity(false);
}
}
})
}
else {
this.body.setAllowGravity(true);
}
})
}
//CREATE Intersect function
intersects(rectangleBody, triangle){
return Phaser.Geom.Intersects.RectangleToTriangle(rectangleBody.getBounds(), triangle);
}
update(time, delta){
this.handleMovement();
this.handleInterSection();
}
}