-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.js
More file actions
91 lines (89 loc) · 2.85 KB
/
Entity.js
File metadata and controls
91 lines (89 loc) · 2.85 KB
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
import { vec3 } from "./math.js";
class Entity {
constructor(hitboxes, {
eyePos = [
(hitboxes.min[0] + hitboxes.min[0]) / 2,
(hitboxes.min[1] + hitboxes.min[1]) / 2,
(hitboxes.min[2] + hitboxes.min[2]) / 2
],
pitch = 0, yaw = 0,
position = [0, 0, 0],
world = null,
controller = null,
camera = null,
uid = performance.timeOrigin + performance.now() + "-" + Math.random(),
} = {}) {
this.moveSpeed = 0;
this.gravityAcceleration = 0;
this.position = vec3.create(...position);
this.pitch = pitch; // 垂直角 始于xz平面 以y+为正方向 弧度制
this.yaw = yaw; // 水平角 始于z- 以x-为正方向 弧度制
this.hitboxes = hitboxes;
this.eyePos = vec3.create(...eyePos);
this.forward = vec3.create();
this.direction = vec3.create();
this.model = null;
this.setWorld(world);
this.setCamera(camera);
this.setController(controller);
this.uid = uid;
this.type = "Entity";
};
getEyePosition() {
return vec3.add(this.eyePos, this.position);
};
getGloBox() {
return {
min: vec3.add(this.hitboxes.min, this.position),
max: vec3.add(this.hitboxes.max, this.position)
};
};
getDirection(scale = 1) {
return vec3(this.direction)
.create(0, 0, -scale)
.rotateX(this.pitch)
.rotateY(this.yaw).res;
};
setCamera(camera = null) {
if (camera === this.camera) return this;
const lastCamera = this.camera;
this.camera = camera;
if (lastCamera) lastCamera.bindEntity(null);
if (camera) camera.bindEntity(this);
return this;
};
setController(controller = null) {
if (controller === this.controller) return this;
const lastController = this.controller;
this.controller = controller;
if (lastController) lastController.setEntity(null);
if (controller) controller.setEntity(this);
return this;
};
setWorld(world = null) {
this.world = world;
return this;
};
onRender(timestamp, dt) {};
onTick() {};
toObj() {
const typedArr2arr = ta => Array.from(ta);
return {
moveSpeed: this.moveSpeed,
gravityAcceleration: this.gravityAcceleration,
position: typedArr2arr(this.position),
pitch: this.pitch,
yaw: this.yaw,
hitboxes: { min: this.hitboxes.min, max: this.hitboxes.max },
eyePos: typedArr2arr(this.eyePos),
forward: typedArr2arr(this.forward),
direction: typedArr2arr(this.direction),
uid: this.uid,
type: this.type,
};
};
};
export {
Entity,
Entity as default
};