-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
2 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import "mocha" | ||
import { expect } from "chai" | ||
import { HitEvent } from "../../src/controller/controller" | ||
import { Container } from "../../src/container/container" | ||
import { GameEvent } from "../../src/events/gameevent" | ||
import { initDom } from "../view/dom" | ||
import { Init } from "../../src/controller/init" | ||
import { TableGeometry } from "../../src/view/tablegeometry" | ||
import { Vector3 } from "three" | ||
import { R } from "../../src/model/physics/constants" | ||
import { norm, round, roundVec } from "../../src/utils/utils" | ||
|
||
initDom() | ||
|
||
const jestConsole = console | ||
|
||
beforeEach(() => { | ||
global.console = require("console") | ||
}) | ||
|
||
afterEach(() => { | ||
global.console = jestConsole | ||
}) | ||
|
||
|
||
describe("Controller", () => { | ||
let container: Container | ||
let broadcastEvents: GameEvent[] | ||
let stepx: number | ||
let stepy: number | ||
//const replayUrl = "http://localhost:8080/?ruletype=threecushion&state=" | ||
const replayUrl = "?ruletype=threecushion&state=" | ||
beforeEach(function (done) { | ||
container = new Container( | ||
document.getElementById("viewP1"), | ||
(_) => {}, | ||
false, | ||
"threecushion" | ||
) | ||
broadcastEvents = [] | ||
container.broadcast = (x) => broadcastEvents.push(x) | ||
stepx = TableGeometry.X / 4 | ||
stepy = TableGeometry.Y / 2 | ||
done() | ||
}) | ||
|
||
const toLowerRail = new Vector3(0,-3*R) | ||
const toUpperRail = new Vector3(0,3*R) | ||
|
||
it("initialise ball for diamond system shot", (done) => { | ||
expect(container.controller).to.be.an.instanceof(Init) | ||
container.table.cue.aim.power = 2 | ||
container.table.cue.aim.offset.x = -0.3 | ||
shot(gridPosition(0,0),gridPosition(5,4)) | ||
shot(gridPosition(2,0),gridPosition(6,4)) | ||
shot(gridPosition(4,0),gridPosition(7,4)) | ||
shot(gridPosition(6,0),gridPosition(8,4)) | ||
done() | ||
}) | ||
|
||
function shot(fromDiamond,toDiamond) { | ||
const start = fromDiamond.add(toLowerRail) | ||
const target = toDiamond.add(toUpperRail) | ||
playAlong(start,target) | ||
console.log(getURL()) | ||
} | ||
|
||
function playAlong(start,target) { | ||
const dir = norm(target.clone().sub(start)) | ||
const ballStart = start.clone().addScaledVector(dir,R*7) | ||
container.table.cueball.pos.copy(ballStart) | ||
container.table.cue.aim.angle = round(Math.atan2(dir.y,dir.x)) | ||
} | ||
|
||
function gridPosition(x,y) { | ||
return new Vector3((-4+x) * stepx,(-2+y) * stepy,0) | ||
} | ||
|
||
function getURL() { | ||
roundVec(container.table.cueball.pos) | ||
container.table.cue.moveTo(container.table.cueball.pos) | ||
const event: HitEvent = new HitEvent(container.table.serialise()) | ||
container.recoder.record(event) | ||
container.recoder.updateBreak([]) | ||
|
||
const state = container.recoder.replayLastShot() | ||
const shotUri = `${replayUrl}${encodeURIComponent(JSON.stringify(state))}` | ||
return shotUri | ||
} | ||
}) |