Skip to content

Commit

Permalink
Fix few bugs & add basic refresh button
Browse files Browse the repository at this point in the history
  • Loading branch information
phisn committed Sep 22, 2024
1 parent b78d9a8 commit a1da6c8
Show file tree
Hide file tree
Showing 104 changed files with 22,448 additions and 7,497 deletions.
20,878 changes: 20,878 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions packages-dev/eslint-config-custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
project: true,
},
rules: {
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/unbound-method": "off",
Expand All @@ -34,15 +35,15 @@ module.exports = {
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
args: "all",
argsIgnorePattern: "^_",
caughtErrors: "all",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
varsIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
},
ignorePatterns: ["*.cjs", "vite.config.ts", "rollup.config.mjs"],
}
14 changes: 14 additions & 0 deletions packages/game/src/framework/entity.base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,20 @@ describe("EntityStore", () => {
expect(added).toHaveBeenCalledTimes(1)
})

it("should show new entity in single", () => {
store.create({ position: { x: 0, y: 0 } })
const single = store.single("position")

for (const retrieved of store.multipleCopy("position")) {
store.remove(retrieved)
}

const newEntity = store.create({ position: { x: 1, y: 1 } })
const retrieved = single()

expect(retrieved.id).toBe(newEntity.id)
})

it("should handle multiple listeners with different requirements", () => {
const entity = store.create({ position: { x: 0, y: 0 } })

Expand Down
48 changes: 4 additions & 44 deletions packages/game/src/framework/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,6 @@ export interface EntityStoreScoped<Components extends object> extends EntityStor
clear(): void
}

class WeakLookup<K, T extends WeakKey> {
private map = new Map<K, WeakRef<T>>()
private inUse = false

set(key: K, value: any) {
this.map.set(key, new WeakRef(value))
}

get(key: K) {
const value = this.map.get(key)

if (value === undefined) {
return undefined
}

const result = value.deref()

if (result === undefined) {
this.map.delete(key)
}

return result
}
}

export function newEntityStore<Components extends object>(): EntityStore<Components> {
function componentsToKey(components: string[]) {
return components.sort().join(",")
Expand Down Expand Up @@ -484,7 +459,7 @@ export function newEntityStore<Components extends object>(): EntityStore<Compone
return () => void newListener.remove(symbol)
}

const multiples = new WeakLookup<string, readonly Entity<any, Components>[]>()
const multiples = new Map<string, readonly Entity<any, Components>[]>()

function multiple<C extends ComponentsWithModifier<Components>[]>(
...requirements: string[]
Expand All @@ -497,29 +472,14 @@ export function newEntityStore<Components extends object>(): EntityStore<Compone
}

const entityList = new EntityList(requirements)
const entityListRef = new WeakRef(entityList)

const unlisten = listen(
listen(
requirements,
entity => {
const entityList = entityListRef.deref()

if (entityList === undefined) {
unlisten()
return
}

entityList.push(entity)
},
entity => {
const result = entityListRef.deref()

if (result === undefined) {
unlisten()
return
}

result.remove(entity)
entityList.remove(entity)
},
)

Expand All @@ -534,7 +494,7 @@ export function newEntityStore<Components extends object>(): EntityStore<Compone
return [...multiple(...requirements)]
}

const keyToSingle = new WeakLookup<string, any>()
const keyToSingle = new Map<string, any>()

function single<C extends ComponentsWithModifier<Components>[]>(
...requirements: string[]
Expand Down
11 changes: 6 additions & 5 deletions packages/game/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ export class Game {

if (summary.flags === 0) {
this.reset = true
this.started = false
}
},
})
}

public onUpdate(input: GameInput) {
if (this.reset) {
this.onReset()
this.reset = false
}

if (this.started === false) {
if (input.thrust === false) {
return
Expand All @@ -63,11 +69,6 @@ export class Game {
this.started = true
}

if (this.reset) {
this.onReset()
this.reset = false
}

this.moduleLevel.onUpdate(input)
this.moduleRocket.onUpdate(input)
this.moduleWorld.onUpdate(input)
Expand Down
5 changes: 4 additions & 1 deletion packages/game/src/modules/module-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ModuleLevel {
}

onUpdate(_input: GameInput) {
if (this.progress && this.progressLevel) {
if (this.progress !== undefined && this.progressLevel) {
this.progress -= 1

if (this.progress <= 0) {
Expand All @@ -87,9 +87,12 @@ export class ModuleLevel {
Math.abs(rocketVelocity.x) > ROCKET_SPEED_TOLERANCE ||
Math.abs(rocketVelocity.y) > ROCKET_SPEED_TOLERANCE
) {
console.log("Rocket is moving too fast to capture the flag")
return
}

console.log("Captured flag")

const captured = level
this.progressLevel = undefined

Expand Down
28 changes: 20 additions & 8 deletions packages/game/src/modules/module-rocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,27 @@ export class ModuleRocket {
})
}

onUpdate({ thrust, rotation }: GameInput) {
onUpdate(input: GameInput) {
this.previousInput = input

const rocket = this.getRocket()

const rocketComponent = rocket.get("rocket")
const body = rocket.get("body")

if (this.firstInput) {
this.firstInput = false
rocketComponent.rotationWithoutInput = -rotation
rocketComponent.rotationWithoutInput = -input.rotation
}

rocketComponent.framesSinceLastDeath++
rocketComponent.thrust = thrust
rocketComponent.thrust = input.thrust

if (rocketComponent.collisionCount === 0) {
body.setRotation(rotation + rocketComponent.rotationWithoutInput, true)
body.setRotation(input.rotation + rocketComponent.rotationWithoutInput, true)
}

if (thrust) {
if (input.thrust) {
const rapier = this.store.resources.get("rapier")
const world = this.store.resources.get("world")

Expand Down Expand Up @@ -191,9 +193,8 @@ export class ModuleRocket {
rocketComponent.collisionCount--
}

if (rocketComponent.collisionCount === 0 && this.previousInput) {
rocketComponent.rotationWithoutInput =
rocket.get("body").rotation() - this.previousInput.rotation
if (rocketComponent.collisionCount === 0) {
this.resetRotation(rocket)
}
}

Expand Down Expand Up @@ -293,6 +294,17 @@ export class ModuleRocket {

body.setLinvel({ x: 0, y: 0 }, false)
body.setAngvel(0, true)

this.resetRotation(rocket)
}

private resetRotation(rocket: RocketEntity) {
if (this.previousInput) {
const rocketComponent = rocket.get("rocket")

rocketComponent.rotationWithoutInput =
rocket.get("body").rotation() - this.previousInput.rotation
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions packages/runtime-framework/.vscode/settings.json

This file was deleted.

3 changes: 0 additions & 3 deletions packages/runtime-framework/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions packages/runtime-framework/package.json

This file was deleted.

Loading

0 comments on commit a1da6c8

Please sign in to comment.