Add a simple ball (#24)
This commit is contained in:
1 parent
d89d954f8c
commit
ebd85d4c05
7 files changed
+178
-12
No files matched your search
@@ -0,0 +1,138 @@
|
||||
|
||||
import { Actor, ActorArgs, Circle, Color, Engine, Graphic, GraphicsGroup, Random, vec, Vector } from "excalibur";
|
||||
import { assertNotNull, assertNull } from "../utils/assertUtils";
|
||||
import { gravitationalConstant, groundHeight } from "../utils/physicsConstant";
|
||||
|
||||
export const defaultBallConfiguration: BallConfiguration = {
|
||||
color: Color.Orange,
|
||||
radius: 5,
|
||||
}
|
||||
|
||||
export interface BallConfiguration {
|
||||
readonly color: Color;
|
||||
readonly radius: number;
|
||||
}
|
||||
|
||||
const random = new Random();
|
||||
|
||||
export class Ball extends Actor {
|
||||
private _graphic: Graphic = null!;
|
||||
private _animation: BallAnimation = new BallAnimation(this);
|
||||
|
||||
constructor(actorArgs: ActorArgs, public readonly configuration: BallConfiguration) {
|
||||
super(actorArgs);
|
||||
}
|
||||
|
||||
|
||||
onInitialize(engine: Engine): void {
|
||||
const { configuration } = this;
|
||||
const { radius } = configuration;
|
||||
|
||||
const ballGraphic = new Circle({
|
||||
radius: radius,
|
||||
color: configuration.color,
|
||||
});
|
||||
|
||||
const specularGraphic = new Circle({
|
||||
radius: radius / 5,
|
||||
color: Color.White,
|
||||
});
|
||||
|
||||
this._graphic = new GraphicsGroup({
|
||||
members: [
|
||||
{ graphic: ballGraphic, offset: vec(0, 0)},
|
||||
{ graphic:specularGraphic, offset: vec(radius / 2, radius / 2)}
|
||||
],
|
||||
})
|
||||
|
||||
this.graphics.add(this._graphic);
|
||||
|
||||
const start = vec(-150, -200);
|
||||
this.pos = start;
|
||||
|
||||
engine.input.pointers.primary.on("down", () => {
|
||||
const randomVelocity = vec(random.integer(100, 400), random.integer(0, -400));
|
||||
this.startParabolaAnimation(new Parabola(start, randomVelocity));
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
public update(engine: Engine, elapsed: number): void {
|
||||
super.update(engine, elapsed);
|
||||
this._animation.update(elapsed);
|
||||
}
|
||||
|
||||
startParabolaAnimation(parabola: Parabola): void {
|
||||
this._animation.start(parabola);
|
||||
}
|
||||
}
|
||||
|
||||
class BallAnimation {
|
||||
private _running: boolean = false;
|
||||
private _parabola: Parabola | null = null;
|
||||
private _elapsedTime: number | null = null;
|
||||
|
||||
constructor(private _ball: Ball) {
|
||||
|
||||
}
|
||||
|
||||
get running(): boolean {
|
||||
return this._running;
|
||||
}
|
||||
|
||||
start(parabola: Parabola): void {
|
||||
this.reset();
|
||||
this._parabola = parabola;
|
||||
this._running = true;
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this._parabola = null;
|
||||
this._running = false;
|
||||
this._elapsedTime = null;
|
||||
|
||||
// TODO: Reset ball position?
|
||||
}
|
||||
|
||||
update(elapsed: number): void {
|
||||
if (!this.running) {
|
||||
assertNull(this._parabola);
|
||||
assertNull(this._elapsedTime);
|
||||
return;
|
||||
}
|
||||
|
||||
assertNotNull(this._parabola);
|
||||
|
||||
if (this._elapsedTime == null) {
|
||||
this._elapsedTime = 0;
|
||||
}
|
||||
this._elapsedTime += elapsed;
|
||||
|
||||
const newPosition = this._parabola.position(this._elapsedTime);
|
||||
|
||||
// Collide with ground
|
||||
const yMin = groundHeight - this._ball.configuration.radius;
|
||||
newPosition.y = Math.min(yMin, newPosition.y);
|
||||
|
||||
this._ball.pos = newPosition;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class Parabola {
|
||||
constructor(
|
||||
readonly startPos: Vector,
|
||||
readonly startVelocity: Vector
|
||||
) { }
|
||||
|
||||
position(time: number): Vector {
|
||||
const timeSeconds = time / 1000;
|
||||
|
||||
const x = this.startPos.x + this.startVelocity.x * timeSeconds;
|
||||
const y = this.startVelocity.y * timeSeconds + gravitationalConstant / 2 * timeSeconds ** 2 + this.startPos.y;
|
||||
|
||||
return new Vector(x, y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
|
||||
import { Actor, ActorArgs, Color, Engine, Graphic, GraphicsGroup, Line, Polygon,vec } from "excalibur";
|
||||
import { pixelsPerMeter } from "../utils/physicsConstant";
|
||||
|
||||
export const defaultCupConfiguration = {
|
||||
cupColor: Color.Red,
|
||||
rimColor: Color.White,
|
||||
widthRim: 18,
|
||||
widthBottom: 12,
|
||||
height: 20,
|
||||
rimThickness: 2,
|
||||
widthRim: 0.18 * pixelsPerMeter,
|
||||
widthBottom: 0.12 * pixelsPerMeter,
|
||||
height: 0.2 * pixelsPerMeter,
|
||||
rimThickness: 0.02 * pixelsPerMeter,
|
||||
}
|
||||
|
||||
export interface CupConfiguration {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur";
|
||||
import { groundHeight } from "../utils/physicsConstant";
|
||||
|
||||
const groundColor = Color.Gray;
|
||||
|
||||
@@ -15,7 +16,7 @@ export class Ground extends Actor {
|
||||
})
|
||||
this.graphics.add(this._graphic);
|
||||
|
||||
this.pos.setTo(0, 0);
|
||||
this.pos.setTo(0, groundHeight);
|
||||
this.anchor.setTo(0.5, 0);
|
||||
|
||||
this._graphic.width = 10000;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Actor, ActorArgs, Color, Engine, Graphic, Polygon,vec } from "excalibur";
|
||||
import { pixelsPerMeter } from "../utils/physicsConstant";
|
||||
|
||||
export const defaultTableConfiguration = {
|
||||
color: Color.Brown,
|
||||
width: 300,
|
||||
height: 60,
|
||||
thickness: 10,
|
||||
legOffset: 20,
|
||||
legThickness: 10,
|
||||
width: 2.44 * pixelsPerMeter,
|
||||
height: 0.7 * pixelsPerMeter,
|
||||
thickness: 0.15 * pixelsPerMeter,
|
||||
legOffset: 0.15 * pixelsPerMeter,
|
||||
legThickness: 0.15 * pixelsPerMeter,
|
||||
}
|
||||
|
||||
export interface TableConfiguration {
|
||||
|
||||
@@ -4,10 +4,13 @@ import { Ground } from "../GameElements/Ground";
|
||||
import { defaultTableConfiguration, Table } from "../GameElements/Table";
|
||||
import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
|
||||
import { Color, vec } from "excalibur";
|
||||
import { Ball, defaultBallConfiguration } from "../GameElements/Ball";
|
||||
import { groundHeight } from "../utils/physicsConstant";
|
||||
|
||||
export class GameScene extends GameStateScene {
|
||||
private _ground: Ground = null!;
|
||||
private _table: Table = null!;
|
||||
private _ball: Ball = null!;
|
||||
|
||||
private _cups: Cup[] = [];
|
||||
|
||||
@@ -22,7 +25,7 @@ export class GameScene extends GameStateScene {
|
||||
this._ground = new Ground();
|
||||
this.add(this._ground);
|
||||
|
||||
this._table = new Table({}, defaultTableConfiguration);
|
||||
this._table = new Table({pos: vec(0, groundHeight)}, defaultTableConfiguration);
|
||||
this.add(this._table);
|
||||
|
||||
const tableHalfWidth = this._table.configuration.width / 2;
|
||||
@@ -34,7 +37,7 @@ export class GameScene extends GameStateScene {
|
||||
];
|
||||
cupPositions.forEach(offsetX => {
|
||||
const cup = new Cup({
|
||||
pos: vec(offsetX, -tableHeight),
|
||||
pos: vec(offsetX, groundHeight - tableHeight),
|
||||
color: Color.Red,
|
||||
}, defaultCupConfiguration)
|
||||
|
||||
@@ -42,5 +45,7 @@ export class GameScene extends GameStateScene {
|
||||
this._cups.push(cup);
|
||||
});
|
||||
|
||||
this._ball = new Ball({}, defaultBallConfiguration);
|
||||
this.add(this._ball);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
export function assert(condition: boolean, message?: string): void {
|
||||
if (!condition) {
|
||||
throw new Error(message || "Assertion failed");
|
||||
}
|
||||
}
|
||||
|
||||
export function assertNull<T>(obj: T | null): asserts obj is null {
|
||||
if (obj != null) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
}
|
||||
|
||||
export function assertNotNull<T>(obj: T | null): asserts obj is T {
|
||||
if (obj == null) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export const pixelsPerMeter = 100;
|
||||
export const gravitationalConstant = 9.81 * pixelsPerMeter;
|
||||
export const groundHeight = 0;
|
||||
Reference in new issue
Block a user