refactor: json extends, projectile
This commit is contained in:
parent
6e8f74b369
commit
29471d8b5d
|
@ -6,21 +6,17 @@ export default class Controlled extends Component {
|
|||
directionMove(direction) {
|
||||
const x = Math.cos(direction);
|
||||
if (x > 0) {
|
||||
this.moveLeft = 0;
|
||||
this.moveRight = x;
|
||||
this.moveRight += x;
|
||||
}
|
||||
else {
|
||||
this.moveLeft = -x;
|
||||
this.moveRight = 0;
|
||||
this.moveLeft -= x;
|
||||
}
|
||||
const y = Math.sin(direction);
|
||||
if (y > 0) {
|
||||
this.moveUp = 0;
|
||||
this.moveDown = y;
|
||||
this.moveDown += y;
|
||||
}
|
||||
else {
|
||||
this.moveUp = -y;
|
||||
this.moveDown = 0;
|
||||
this.moveUp -= y;
|
||||
}
|
||||
}
|
||||
stop() {
|
||||
|
|
|
@ -413,7 +413,11 @@ export default class Ecs {
|
|||
|
||||
readJson(uri) {
|
||||
const buffer = this.readAsset(uri);
|
||||
return buffer.byteLength > 0 ? JSON.parse(textDecoder.decode(buffer)) : {};
|
||||
const {$$extends, ...json} = buffer.byteLength > 0 ? JSON.parse(textDecoder.decode(buffer)) : {};
|
||||
return {
|
||||
...json,
|
||||
...$$extends && this.readJson($$extends),
|
||||
};
|
||||
}
|
||||
|
||||
readScript(path, locals = {}) {
|
||||
|
|
7
resources/combat/projectile/base.entity.json
Normal file
7
resources/combat/projectile/base.entity.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"Controlled": {},
|
||||
"Forces": {},
|
||||
"Speed": {},
|
||||
"Ticking": {},
|
||||
"VisibleAabb": {}
|
||||
}
|
11
resources/combat/projectile/diffuse.js
Normal file
11
resources/combat/projectile/diffuse.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import Ticker from '@/util/ticker.js';
|
||||
|
||||
export default function *({Controlled, Direction, Speed}, duration, speed) {
|
||||
let accumulated = 0;
|
||||
while (accumulated <= duration) {
|
||||
Controlled.stop();
|
||||
Controlled.directionMove(Direction.direction);
|
||||
accumulated += yield Ticker.wait();
|
||||
Speed.speed = speed * (1 - (accumulated / duration));
|
||||
}
|
||||
}
|
9
resources/combat/projectile/toward.js
Normal file
9
resources/combat/projectile/toward.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
import * as Math from '@/util/math.js';
|
||||
import Ticker from '@/util/ticker.js';
|
||||
|
||||
export default function*({Controlled, Direction, Position}, {x, y}, duration) {
|
||||
const toward = Math.atan2(y - Position.y, x - Position.x);
|
||||
Direction.direction = (Math.TAU + toward) % Math.TAU;
|
||||
Controlled.directionMove(Direction.direction);
|
||||
yield Ticker.wait(duration);
|
||||
}
|
|
@ -1,22 +1,12 @@
|
|||
import * as Math from '@/util/math.js';
|
||||
import Ticker from '@/util/ticker.js';
|
||||
|
||||
const SPREAD = 0.5;
|
||||
import diffuse from '%/combat/projectile/diffuse.js';
|
||||
import toward from '%/combat/projectile/toward.js';
|
||||
|
||||
export default function*({ecs, entity}) {
|
||||
const {Arbitrary, Controlled, Direction, id, Position, Speed, Sprite} = entity;
|
||||
let accumulated = 0;
|
||||
Controlled.directionMove(Direction.direction);
|
||||
while (accumulated <= SPREAD) {
|
||||
accumulated += yield Ticker.wait();
|
||||
Speed.speed = 100 * (1 - (accumulated / SPREAD));
|
||||
}
|
||||
const {where: {x, y}} = Arbitrary.bag;
|
||||
const toward = Math.atan2(y - Position.y, x - Position.x);
|
||||
Direction.direction = (Math.TAU + toward) % Math.TAU;
|
||||
Controlled.directionMove(Direction.direction);
|
||||
const {Arbitrary, Controlled, id, Speed, Sprite} = entity;
|
||||
yield* diffuse(entity, 0.5, 100);
|
||||
Controlled.stop();
|
||||
Speed.speed = 400;
|
||||
yield Ticker.wait(0.5);
|
||||
yield* toward(entity, Arbitrary.bag.where, 0.5);
|
||||
Sprite.alpha = 0;
|
||||
ecs.destroy(id);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"$$extends": "/resources/combat/projectile/base.entity.json",
|
||||
"Behaving": {
|
||||
"routines": {
|
||||
"initial": "/resources/magic-swords/initial.js"
|
||||
|
@ -30,18 +31,13 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"Controlled": {},
|
||||
"Forces": {},
|
||||
"Harmful": {
|
||||
"harmScript": "/resources/magic-swords/harm.js"
|
||||
},
|
||||
"Light": {
|
||||
"brightness": 0
|
||||
},
|
||||
"Speed": {},
|
||||
"Sprite": {
|
||||
"source": "/resources/magic-swords/magic-sword-shot.sprite.json"
|
||||
},
|
||||
"Ticking": {},
|
||||
"VisibleAabb": {}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import * as Math from '@/util/math.js';
|
||||
import Ticker from '@/util/ticker.js';
|
||||
|
||||
const SHOTS = 14;
|
||||
const SHOTS = 5;
|
||||
|
||||
function createShots(ecs, where, wielder) {
|
||||
const offset = Math.random() * Math.TAU;
|
||||
|
|
|
@ -37,6 +37,10 @@ export default defineConfig({
|
|||
find: '@',
|
||||
replacement: fileURLToPath(new URL('./app', import.meta.url))
|
||||
},
|
||||
{
|
||||
find: '%',
|
||||
replacement: fileURLToPath(new URL('./resources', import.meta.url))
|
||||
},
|
||||
],
|
||||
},
|
||||
server: {
|
||||
|
|
Loading…
Reference in New Issue
Block a user