Skip to content

Commit

Permalink
呼び出しインターフェースを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuyoshi-yamazaki committed Nov 20, 2022
1 parent 4596f69 commit ca5b9ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/simulations/garden/p5_drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class P5Drawer {
}

public drawAll<DrawableState extends AnyDrawableStates>(states: DrawableState[]): void {
this.p.background(0, 0xFF);

[...states]
.sort((lhs, rhs) => drawPriority[lhs.case] - drawPriority[rhs.case])
.forEach(state => this.draw(state))
Expand Down
18 changes: 15 additions & 3 deletions src/simulations/garden/source.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import p5 from "p5"
import { Vector } from "src/classes/physics"
import { defaultCanvasParentId } from "../../react-components/common/default_canvas_parent_id"
import { P5Drawer } from "./p5_drawer"
import { World } from "./world"

let t = 0
const canvasId = "canvas"
const fieldSize = 600
const cellSize = 4
const worldSize = new Vector(200, 200)
const fieldSize = worldSize.mult(cellSize)

export const main = (p: p5): void => {
const world = new World(worldSize)
const drawer = new P5Drawer(p)

p.setup = () => {
const canvas = p.createCanvas(fieldSize, fieldSize)
const canvas = p.createCanvas(fieldSize.x, fieldSize.y)
canvas.id(canvasId)
canvas.parent(defaultCanvasParentId)

p.background(0, 0xFF)
}

p.draw = () => {
p.background(0, 0xFF)
const drawableObjects = [
world.drawableState(),
...world.getDrawableObjects().map(obj => obj.drawableState()),
]
drawer.drawAll(drawableObjects)
t += 1
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/simulations/garden/world.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Vector } from "src/classes/physics"
import { Drawable } from "./drawable"
import { Life } from "./life"
import { Terrain } from "./terrain"

type AnyWorldObject = Terrain | Life

export type WorldDrawableState = {
readonly case: "world"
}

export class World implements Drawable<WorldDrawableState> {

private terrains: Terrain[]

public constructor(
public readonly size: Vector,
Expand All @@ -18,4 +22,11 @@ export class World implements Drawable<WorldDrawableState> {
case: "world",
}
}

public calculate(): void {
}

public getDrawableObjects(): AnyWorldObject[] {
return []
}
}

0 comments on commit ca5b9ca

Please sign in to comment.