Skip to content

Commit

Permalink
Refactor and prototype polyburn gym environment
Browse files Browse the repository at this point in the history
  • Loading branch information
phisn committed May 6, 2024
1 parent 139f909 commit a8cbc20
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 1,000 deletions.
Binary file modified packages/learning-gym/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/learning-gym/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"gl": "^8.0.2",
"pngjs": "^7.0.0",
"custom-rapier2d-node": "*",
"three": "^0.124.0",
"three": "0.145.0",
"tsconfig": "*",
"web-game": "*"
},
Expand Down
229 changes: 143 additions & 86 deletions packages/learning-gym/src/main.ts

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions packages/web-game/src/game/game-agent-as-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as THREE from "three"
import { GameInterface } from "./game"
import { GameAgentWrapper } from "./game-agent-wrapper"
import { GameSettings } from "./game-settings"
import { ModuleInput } from "./modules/module-input/module-input"
import { ExtendedRuntime, newExtendedRuntime } from "./runtime-extension/new-extended-runtime"

export class GameAgentAsPlayer implements GameInterface {
runtime: ExtendedRuntime
input: ModuleInput
gameWrapper: GameAgentWrapper

constructor(settings: GameSettings) {
settings.canvas.width = 64
settings.canvas.height = 64
settings.gamemode = "Normal"

const renderer = new THREE.WebGLRenderer({
canvas: settings.canvas,
antialias: false,
alpha: true,
})

this.runtime = newExtendedRuntime(settings, new THREE.Scene(), renderer)

renderer.setClearColor(THREE.Color.NAMES["black"], 1)

this.gameWrapper = new GameAgentWrapper(this.runtime, this.runtime.factoryContext.scene)
this.input = new ModuleInput(this.runtime)
}

dispose() {
this.input.dispose()
}

onPreFixedUpdate(delta: number) {
this.input.onPreFixedUpdate(delta)
}

onFixedUpdate() {
const context = {
thrust: this.input.thrust(),
rotation: this.input.rotation(),
}

this.gameWrapper.step(context)

this.runtime.factoryContext.renderer.render(
this.runtime.factoryContext.scene,
this.gameWrapper.camera,
)
}

onUpdate() {}
}
54 changes: 2 additions & 52 deletions packages/web-game/src/game/game-agent-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { RuntimeComponents } from "runtime/src/core/runtime-components"
import { RuntimeSystemContext } from "runtime/src/core/runtime-system-stack"
import { Runtime } from "runtime/src/runtime"
import * as THREE from "three"
import { GameInterface } from "./game"
import { GameSettings } from "./game-settings"
import { ModuleInput } from "./modules/module-input/module-input"
import { ModuleSceneAgent } from "./modules/module-scene-agent/module-scene-agent"
import { ExtendedRuntime, newExtendedRuntime } from "./runtime-extension/new-extended-runtime"

export class GameAgentWrapper {
sceneModule: ModuleSceneAgent
Expand All @@ -30,6 +26,8 @@ export class GameAgentWrapper {
this.rocket.components.rigidBody.translation().y,
10,
)

this.sceneModule.onUpdate()
}

step(context: RuntimeSystemContext) {
Expand All @@ -44,51 +42,3 @@ export class GameAgentWrapper {
)
}
}

export class GameAgentAsPlayer implements GameInterface {
runtime: ExtendedRuntime
input: ModuleInput
gameWrapper: GameAgentWrapper

constructor(settings: GameSettings) {
settings.canvas.width = 64
settings.canvas.height = 64

const renderer = new THREE.WebGLRenderer({
canvas: settings.canvas,
antialias: false,
alpha: true,
})

this.runtime = newExtendedRuntime(settings, new THREE.Scene(), renderer)

renderer.setClearColor(THREE.Color.NAMES["black"], 1)

this.gameWrapper = new GameAgentWrapper(this.runtime, this.runtime.factoryContext.scene)
this.input = new ModuleInput(this.runtime)
}

dispose() {
this.input.dispose()
}

onPreFixedUpdate(delta: number) {
this.input.onPreFixedUpdate(delta)
}

onFixedUpdate() {
const context = {
thrust: this.input.thrust(),
rotation: this.input.rotation(),
}

this.gameWrapper.step(context)

this.runtime.factoryContext.renderer.render(
this.runtime.factoryContext.scene,
this.gameWrapper.camera,
)
}

onUpdate() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ export class ModuleSceneAgent {
const tl = currentLevel.components.level.boundsTL
const br = currentLevel.components.level.boundsBR

this.upBound.position.set((tl.x + br.x) / 2, br.y + 87.25, 0)
this.leftBound.position.set(tl.x - 50, (tl.y + br.y) / 2, 0)
this.downBound.position.set((tl.x + br.x) / 2, tl.y - 87.25, 0)
this.rightBound.position.set(br.x + 50, (tl.y + br.y) / 2, 0)
const center = new THREE.Vector2((tl.x + br.x) / 2, (tl.y + br.y) / 2)
const width = Math.abs(br.x - tl.x)
const height = Math.abs(br.y - tl.y)

this.upBound.position.set(center.x, center.y + height / 2 + 50, 0)
this.downBound.position.set(center.x, center.y - height / 2 - 50, 0)
this.leftBound.position.set(center.x - width / 2 - 50, center.y, 0)
this.rightBound.position.set(center.x + width / 2 + 50, center.y, 0)

this.previousCurrentLevel = currentLevel
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/app/play/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useEffect, useState } from "react"
import { WorldModel } from "runtime/proto/world"
import { Game as NativeGame } from "web-game/src/game/game"
import { GameAgentAsPlayer } from "web-game/src/game/game-agent-as-player"
import { GameLoop } from "web-game/src/game/game-loop"
import { GameHooks, GameInstanceType, GameSettings } from "web-game/src/game/game-settings"
import { GameAgentAsPlayer } from "../../../../web-game/src/game/game-agent-wrapper"

export function Game(props: {
worldname: string
Expand Down
Loading

0 comments on commit a8cbc20

Please sign in to comment.