-
Notifications
You must be signed in to change notification settings - Fork 0
/
spells.js
65 lines (58 loc) · 1.61 KB
/
spells.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
import { PointLight } from "three";
import { multiplyVector } from "./vectors.js";
import { applyVelocity } from "./physics.js";
export function createSpellsHandler(scene, fireSpellModel) {
let spells = [];
const lightProvider = createLightProvider(scene);
return {
applyFrame(timeElapsed) {
spells.forEach((spell) => spell.applyFrame(timeElapsed));
},
addFireSpell(startPosition, direction) {
const object = fireSpellModel.clone();
scene.add(object);
const light = lightProvider.provide();
const spell = createSpell({
object,
light,
startPosition,
direction,
});
spells.push(spell);
setTimeout(() => {
scene.remove(object);
const index = spells.indexOf(spell);
spells.splice(index, 1);
}, 3000);
},
};
}
const fireSpellSpeed = 30.0;
export function createSpell({ object, light, startPosition, direction }) {
const velocity = multiplyVector(direction, fireSpellSpeed);
object.position.copy(startPosition);
light.position.copy(startPosition);
return {
applyFrame(timeElapsed) {
applyVelocity(object, velocity, timeElapsed);
light.position.copy(object.position);
},
};
}
function createLightProvider(scene) {
const lights = [];
let lightIndex = 0;
for (let i=0; i < 20; i++) {
const light = new PointLight(0xff0000, 75, 100);
light.position.set(0, 0, 100000);
scene.add(light);
lights.push(light);
}
return {
provide() {
const light = lights[lightIndex];
lightIndex = (lightIndex + 1) % lights.length;
return light;
}
};
}