jasonlooked #1
@ -75,12 +75,6 @@ impl Game {
|
||||
if self.phase != GamePhase::Playing {
|
||||
return Err("Cannot throw now".to_string());
|
||||
}
|
||||
let dx = broom_x - HOUSE_CENTER.0;
|
||||
let dy = broom_y - HOUSE_CENTER.1;
|
||||
let dist = (dx * dx + dy * dy).sqrt();
|
||||
if dist > HOUSE_RADIUS {
|
||||
return Err("Broom must be in the house".to_string());
|
||||
}
|
||||
|
||||
self.active_stones.clear();
|
||||
let trajectory = self.physics.throw(self.turn_team, broom_x, broom_y, weight, curl, friction)?;
|
||||
@ -292,4 +286,14 @@ mod tests {
|
||||
let result = game.handle_throw(turn, 0.5, 38.7, 7, 1, 1.0);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_broom_outside_house() {
|
||||
let mut game = Game::new();
|
||||
game.start();
|
||||
let turn = game.turn_team;
|
||||
// (0.0, 30.0) is well outside HOUSE_RADIUS of HOUSE_CENTER
|
||||
let result = game.handle_throw(turn, 0.0, 30.0, 7, 1, 1.0);
|
||||
assert!(result.is_ok(), "broom outside house should be allowed: {:?}", result.err());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { connect, sendThrow, type NetCallbacks } from './net'
|
||||
import { createRenderer } from './renderer'
|
||||
import { createHud, createVelocitySelector, createCurlSelector, createFrictionSlider } from './hud'
|
||||
import { HOUSE_CENTER, HOUSE_RADIUS, type Team } from './protocol'
|
||||
import { type Team } from './protocol'
|
||||
import { GameModel } from './game-model'
|
||||
|
||||
export function startGame(): void {
|
||||
@ -123,35 +123,15 @@ export function startGame(): void {
|
||||
return { x: e.clientX, y: e.clientY }
|
||||
}
|
||||
|
||||
const constrainBroom = (world: { x: number; y: number }) => {
|
||||
const dx = world.x - HOUSE_CENTER.x
|
||||
const dy = world.y - HOUSE_CENTER.y
|
||||
const dist = Math.sqrt(dx * dx + dy * dy)
|
||||
if (dist > HOUSE_RADIUS) {
|
||||
const ratio = HOUSE_RADIUS / dist
|
||||
return {
|
||||
x: HOUSE_CENTER.x + dx * ratio,
|
||||
y: HOUSE_CENTER.y + dy * ratio,
|
||||
}
|
||||
}
|
||||
return world
|
||||
}
|
||||
|
||||
const isInHouse = (world: { x: number; y: number }) => {
|
||||
const dx = world.x - HOUSE_CENTER.x
|
||||
const dy = world.y - HOUSE_CENTER.y
|
||||
return Math.sqrt(dx * dx + dy * dy) <= HOUSE_RADIUS
|
||||
}
|
||||
|
||||
let lastPanScreenY = 0
|
||||
|
||||
const handleStart = (e: Event) => {
|
||||
const pos = getPos(e as TouchEvent)
|
||||
const world = renderer.screenToWorld(pos.x, pos.y)
|
||||
|
||||
if (model.isMyTurn && isInHouse(world)) {
|
||||
if (model.isMyTurn) {
|
||||
model.isDragging = true
|
||||
model.broom = constrainBroom(world)
|
||||
model.broom = world
|
||||
} else {
|
||||
model.isPanning = true
|
||||
lastPanScreenY = pos.y
|
||||
@ -162,7 +142,7 @@ export function startGame(): void {
|
||||
if (model.isDragging && model.isMyTurn) {
|
||||
e.preventDefault()
|
||||
const pos = getPos(e as TouchEvent)
|
||||
model.broom = constrainBroom(renderer.screenToWorld(pos.x, pos.y))
|
||||
model.broom = renderer.screenToWorld(pos.x, pos.y)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user