-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c484a83
commit a3c656a
Showing
10 changed files
with
236 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,65 @@ | ||
import type { Renderable } from "./render/render"; | ||
|
||
import * as PIXI from "pixi.js"; | ||
|
||
import { Disposable } from "@/common/lifecycle"; | ||
|
||
export class CanvasObject extends Disposable { | ||
public constructor() { | ||
import { colors } from "./render/colors"; | ||
import { Vector, VectorCollection } from "./vector"; | ||
|
||
interface ICanvasObject extends Renderable { | ||
mass: number | ||
velocity: Vector | ||
|
||
applyForce(force: Vector): void | ||
clearForces(): void | ||
} | ||
|
||
export class CanvasObject extends Disposable implements ICanvasObject { | ||
private _forces: VectorCollection = new VectorCollection(); | ||
|
||
public constructor( | ||
protected _obj: PIXI.ContainerChild, | ||
public mass: number, | ||
public velocity: Vector | ||
) { | ||
super(); | ||
} | ||
|
||
public applyForce(force: Vector) { | ||
this._forces.push(force); | ||
} | ||
|
||
public clearForces() { | ||
this._forces.clear(); | ||
} | ||
|
||
public update(delta: number, app: PIXI.Application) { | ||
const sumForce = this._forces.getSum(); | ||
const accelerate = Vector.multiplyScalar(sumForce, 1 / this.mass); | ||
|
||
this.velocity = Vector.add(this.velocity, accelerate); | ||
this._obj.x += this.velocity.x * delta; | ||
this._obj.y -= this.velocity.y * delta; | ||
|
||
app.stage.addChild(this._obj); | ||
} | ||
} | ||
|
||
export class Ball extends CanvasObject { | ||
public constructor() { | ||
super( | ||
new PIXI.Graphics() | ||
.arc(100, 100, 15, 0, 2 * Math.PI) | ||
.fill(colors["black"]), | ||
1, | ||
new Vector(0, 0) | ||
); | ||
|
||
this.applyForce(new Vector(0, -2)); | ||
} | ||
|
||
public update(delta: number, app: PIXI.Application) { | ||
super.update(delta, app); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export type Color = string; | ||
export type Color = number; | ||
|
||
export const colors = { | ||
"transparent": "", | ||
"black": "#222", | ||
"skyBlue": "#7dd3fc", | ||
"transparent": NaN, | ||
"black": 0x222222, | ||
"skyBlue": 0x7dd3fc, | ||
} as Record<string, Color>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.