-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
186 lines (161 loc) · 5.33 KB
/
core.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const brownLogic = {
update(entities) {
for (const entityId in entities) {
const entity = entities[entityId]
if (entity.type !== 'brown') {
continue
}
switch (entity.state) {
case 'idle':
entity.stateTick++
if (entity.stateTick === 40) {
entity.stateTick = 0
}
break
case 'move':
entity.stateTick++
if (entity.stateTick === 80) {
entity.state = 'idle'
entity.stateTick = 0
entity.moveCount = 0
}
break
case 'fire':
entity.stateTick++
if (!entity.hitFlag && entity.stateTick >= 15 && entity.stateTick < 20) {
for (const other of Object.values(entities)) {
if (other === entity) {
continue
}
if (other.state === 'cry') {
continue
}
if (Math.pow(other.position.x - entity.position.x, 2) + Math.pow(other.position.z - entity.position.z, 2) <= Math.pow(30, 2)) {
other.velocity.x += 9 * Math.cos(entity.angle)
other.velocity.z += 9 * Math.sin(entity.angle)
entity.velocity.x -= 3 * Math.cos(entity.angle)
entity.velocity.z -= 3 * Math.sin(entity.angle)
entity.hitFlag = true
break
}
}
}
if (entity.stateTick === 80) {
entity.state = 'idle'
entity.stateTick = 0
entity.hitFlag = false
}
break
case 'cry':
entity.stateTick++
if (entity.stateTick === 30) {
entity.stateTick = 0
}
break
}
}
},
move(entity, angle) {
if (entity.state === 'idle' || (entity.state === 'move' && entity.moveCount < entity.maxMoveCount && entity.stateTick >= 5)) {
entity.state = 'move'
entity.stateTick = 0
entity.moveCount++
entity.velocity.x += entity.movePoint * Math.cos(angle)
entity.velocity.z += entity.movePoint * Math.sin(angle)
if (Math.abs(angle) > Math.PI / 2) {
entity.direction.x = -1
}
else {
entity.direction.x = 1
}
if (angle < 0) {
entity.direction.z = -1
}
else {
entity.direction.z = 1
}
}
},
fire(entities, entity, angle) {
if (entity.state === 'idle' || entity.state === 'move') {
entity.state = 'fire'
entity.stateTick = 0
entity.moveCount = 0 //
entity.angle = angle
if (Math.abs(angle) > Math.PI / 2) {
entity.direction.x = -1
}
else {
entity.direction.x = 1
}
if (angle < 0) {
entity.direction.z = -1
}
else {
entity.direction.z = 1
}
}
},
}
const translateLogic = {
value: 0.95,
update(entities) {
for (const entityId in entities) {
const entity = entities[entityId]
entity.velocity.x *= this.value
entity.velocity.z *= this.value
entity.position.x += entity.velocity.x
entity.position.z += entity.velocity.z
}
}
}
const judgmentLogic = {
centerX: 250,
centerZ: 250,
radius: 200,
update(entities) {
for (const entityId in entities) {
const entity = entities[entityId]
if (entity.type !== 'brown') {
continue
}
if (entity.state === 'cry') {
continue
}
if (Math.pow(entity.position.x - this.centerX, 2) + Math.pow(entity.position.z - this.centerZ, 2) >= Math.pow(this.radius, 2)) {
entity.state = 'cry'
entity.stateTick = 0
}
}
}
}
const playerEnter = (entities, data) => {
const entityId = data.entityId
const entity = data.entity
entities[entityId] = entity
}
const playerExit = (entities, data) => {
const entityId = data.entityId
delete entities[entityId]
}
const playerCommand = (entities, data) => {
const commandType = data.commandType
const entityId = data.entityId
const angle = data.angle
switch (commandType) {
case 'move':
brownLogic.move(entities[entityId], angle)
break
case 'fire':
brownLogic.fire(entities, entities[entityId], angle)
break
}
}
module.exports = {
brownLogic,
translateLogic,
judgmentLogic,
playerEnter,
playerExit,
playerCommand
}