-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.ts
128 lines (93 loc) · 2.61 KB
/
Player.ts
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
/*
*/
import { BaseTexture, Rectangle, Sprite, Texture } from 'pixi.js';
import { textSpanIntersectsWithPosition } from 'typescript';
import { Res } from '../../Res';
import { Map } from '../map/Map'
export class Player extends Sprite {
textures: Array<Texture>;
running: boolean = false;
direction: number = 0;
// front
// left
// right
// back
state: number = 0;
// right
// idle
// left
speed: number = 2;
static char:number = 0;
constructor() {
super();
this.textures = Res.getChar(Player.char)
this.x = 50;
this.y = 50;
this.anchor.set(0.5,0.8)
this.texture = this.textures[0]
document.addEventListener("keydown", (ev: KeyboardEvent) => {
const n = Player.getKey(ev.keyCode);
if (n != -1) {
this.direction = n;
this.running = true;
}
});
document.addEventListener("keyup", (ev: KeyboardEvent) => {
const n = Player.getKey(ev.keyCode);
if (n != -1) {
if (n == this.direction) {
this.running = false;
}
}
});
}
setState = ()=>{
if(this.running){
this.state = ((Math.floor(Date.now()/150))%3)
}else{
this.state = 1;
}
// console.log(this.state)
}
setTexture() {
this.texture = this.textures[this.direction * 3 + this.state];
//console.log(this.x,this.y)
}
update = ()=> {
//console.log("iter")
const x = this.x
const y = this.y
if (this.running) {
switch (this.direction) {
case 0:
this.y += this.speed;
break;
case 1:
this.x -= this.speed;
break;
case 2:
this.x += this.speed;
break;
case 3:
this.y -= this.speed;
break;
}
}
if(!Map.tiles.getTile(Math.floor(this.x/16),Math.floor(this.y/16)).walk ){
this.x = x
this.y = y
}
this.setState()
this.setTexture()
}
static getKey(n: number): number {
const KeyPair = [40, 37, 39, 38];
for (let i = 0; i < KeyPair.length; i++) {
if (KeyPair[i] == n) {
console.log("changed dir")
return i;
}
}
return -1;
}
}