@maco-online/head-clash-rules 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +163 -2
- package/dist/definition.d.ts +16 -0
- package/dist/definition.js +42 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/net/predicted-sim.d.ts +21 -0
- package/dist/net/predicted-sim.js +66 -0
- package/dist/net/wire.d.ts +52 -0
- package/dist/net/wire.js +266 -0
- package/dist/room-match.d.ts +35 -0
- package/dist/room-match.js +71 -0
- package/dist/sim/anim.d.ts +7 -0
- package/dist/sim/anim.js +44 -0
- package/dist/sim/ball.d.ts +9 -0
- package/dist/sim/ball.js +464 -0
- package/dist/sim/bot.d.ts +5 -0
- package/dist/sim/bot.js +130 -0
- package/dist/sim/characters.d.ts +22 -0
- package/dist/sim/characters.js +42 -0
- package/dist/sim/constants.d.ts +156 -0
- package/dist/sim/constants.js +166 -0
- package/dist/sim/debug.d.ts +14 -0
- package/dist/sim/debug.js +34 -0
- package/dist/sim/events.d.ts +79 -0
- package/dist/sim/events.js +12 -0
- package/dist/sim/geometry.d.ts +33 -0
- package/dist/sim/geometry.js +58 -0
- package/dist/sim/index.d.ts +18 -0
- package/dist/sim/index.js +13 -0
- package/dist/sim/input.d.ts +12 -0
- package/dist/sim/input.js +25 -0
- package/dist/sim/match.d.ts +17 -0
- package/dist/sim/match.js +208 -0
- package/dist/sim/player.d.ts +16 -0
- package/dist/sim/player.js +207 -0
- package/dist/sim/powerups.d.ts +33 -0
- package/dist/sim/powerups.js +189 -0
- package/dist/sim/rng.d.ts +12 -0
- package/dist/sim/rng.js +21 -0
- package/dist/sim/specials.d.ts +21 -0
- package/dist/sim/specials.js +114 -0
- package/dist/sim/state.d.ts +125 -0
- package/dist/sim/state.js +1 -0
- package/dist/sim/types.d.ts +31 -0
- package/dist/sim/types.js +30 -0
- package/dist/sim/walls.d.ts +60 -0
- package/dist/sim/walls.js +215 -0
- package/package.json +56 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { GoalState, PlayerState } from './state.ts';
|
|
2
|
+
import type { Side } from './types.ts';
|
|
3
|
+
export interface Circle {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
r: number;
|
|
7
|
+
}
|
|
8
|
+
export interface Rect {
|
|
9
|
+
left: number;
|
|
10
|
+
top: number;
|
|
11
|
+
right: number;
|
|
12
|
+
bottom: number;
|
|
13
|
+
}
|
|
14
|
+
/** The head, grown or shrunk by `headScale` around the character's neck point (see CHARACTER_HEADS). */
|
|
15
|
+
export declare function headCircle(p: Readonly<PlayerState>): Circle;
|
|
16
|
+
/** The upper body on the neck point, so no ball slips between the body and a head that sits high. */
|
|
17
|
+
export declare function chestCircle(p: Readonly<PlayerState>): Circle;
|
|
18
|
+
export declare function bodyCircle(p: Readonly<PlayerState>): Circle;
|
|
19
|
+
/** The foot strikes during the middle of the kick. */
|
|
20
|
+
export declare function kickActive(p: Readonly<PlayerState>): boolean;
|
|
21
|
+
/** The foot in front of the body: further out and larger while a kick strikes. */
|
|
22
|
+
export declare function footCircle(p: Readonly<PlayerState>): Circle;
|
|
23
|
+
/** The crossbar of goal `side` (0 left, 1 right). */
|
|
24
|
+
export declare function crossbarRect(goal: Readonly<GoalState>, side: Side): Rect;
|
|
25
|
+
/** The shield wall in front of goal `side`, from its crossbar to the ground. */
|
|
26
|
+
export declare function shieldRect(goal: Readonly<GoalState>, side: Side): Rect;
|
|
27
|
+
export declare const distance: (ax: number, ay: number, bx: number, by: number) => number;
|
|
28
|
+
export declare function circlesOverlap(a: Readonly<Circle>, b: Readonly<Circle>): boolean;
|
|
29
|
+
/** The point of `rect` closest to (x, y). */
|
|
30
|
+
export declare function closestPoint(rect: Readonly<Rect>, x: number, y: number): {
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { CHARACTER_HEADS } from "./characters.js";
|
|
2
|
+
import { BODY_CENTER_Y, BODY_RADIUS, CHEST_RADIUS, CROSSBAR_THICKNESS, FOOT_FORWARD, FOOT_HEIGHT, FOOT_RADIUS, GROUND_Y, HEAD_RADIUS, KICK_ACTIVE_FROM, KICK_ACTIVE_TO, KICK_FOOT_FORWARD, KICK_FOOT_HEIGHT, KICK_FOOT_RADIUS, SHIELD_GAP, SHIELD_THICKNESS, WORLD_W } from "./constants.js";
|
|
3
|
+
/** The head, grown or shrunk by `headScale` around the character's neck point (see CHARACTER_HEADS). */
|
|
4
|
+
export function headCircle(p) {
|
|
5
|
+
const { head, neck } = CHARACTER_HEADS[p.character];
|
|
6
|
+
return { x: p.x, y: p.y - (neck + (head - neck) * p.headScale), r: HEAD_RADIUS * p.headScale };
|
|
7
|
+
}
|
|
8
|
+
/** The upper body on the neck point, so no ball slips between the body and a head that sits high. */
|
|
9
|
+
export function chestCircle(p) {
|
|
10
|
+
return { x: p.x, y: p.y - CHARACTER_HEADS[p.character].neck, r: CHEST_RADIUS };
|
|
11
|
+
}
|
|
12
|
+
export function bodyCircle(p) {
|
|
13
|
+
return { x: p.x, y: p.y - BODY_CENTER_Y, r: BODY_RADIUS };
|
|
14
|
+
}
|
|
15
|
+
/** The foot strikes during the middle of the kick. */
|
|
16
|
+
export function kickActive(p) {
|
|
17
|
+
return p.kickTicks <= KICK_ACTIVE_FROM && p.kickTicks >= KICK_ACTIVE_TO;
|
|
18
|
+
}
|
|
19
|
+
/** The foot in front of the body: further out and larger while a kick strikes. */
|
|
20
|
+
export function footCircle(p) {
|
|
21
|
+
return kickActive(p)
|
|
22
|
+
? { x: p.x + p.facing * KICK_FOOT_FORWARD, y: p.y - KICK_FOOT_HEIGHT, r: KICK_FOOT_RADIUS }
|
|
23
|
+
: { x: p.x + p.facing * FOOT_FORWARD, y: p.y - FOOT_HEIGHT, r: FOOT_RADIUS };
|
|
24
|
+
}
|
|
25
|
+
/** The crossbar of goal `side` (0 left, 1 right). */
|
|
26
|
+
export function crossbarRect(goal, side) {
|
|
27
|
+
const top = goal.crossbarY;
|
|
28
|
+
const bottom = top + CROSSBAR_THICKNESS;
|
|
29
|
+
return side === 0 ? { left: 0, top, right: goal.depth, bottom } : { left: WORLD_W - goal.depth, top, right: WORLD_W, bottom };
|
|
30
|
+
}
|
|
31
|
+
/** The shield wall in front of goal `side`, from its crossbar to the ground. */
|
|
32
|
+
export function shieldRect(goal, side) {
|
|
33
|
+
const top = goal.crossbarY;
|
|
34
|
+
if (side === 0) {
|
|
35
|
+
const left = goal.depth + SHIELD_GAP;
|
|
36
|
+
return { left, top, right: left + SHIELD_THICKNESS, bottom: GROUND_Y };
|
|
37
|
+
}
|
|
38
|
+
const right = WORLD_W - goal.depth - SHIELD_GAP;
|
|
39
|
+
return { left: right - SHIELD_THICKNESS, top, right, bottom: GROUND_Y };
|
|
40
|
+
}
|
|
41
|
+
export const distance = (ax, ay, bx, by) => {
|
|
42
|
+
const dx = bx - ax;
|
|
43
|
+
const dy = by - ay;
|
|
44
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
45
|
+
};
|
|
46
|
+
export function circlesOverlap(a, b) {
|
|
47
|
+
const dx = b.x - a.x;
|
|
48
|
+
const dy = b.y - a.y;
|
|
49
|
+
const reach = a.r + b.r;
|
|
50
|
+
return dx * dx + dy * dy < reach * reach;
|
|
51
|
+
}
|
|
52
|
+
/** The point of `rect` closest to (x, y). */
|
|
53
|
+
export function closestPoint(rect, x, y) {
|
|
54
|
+
return {
|
|
55
|
+
x: Math.min(Math.max(x, rect.left), rect.right),
|
|
56
|
+
y: Math.min(Math.max(y, rect.top), rect.bottom)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type { AnimState, BallMode, CharacterId, Difficulty, InputFrame, Phase, PowerUpKind, Side, SpecialKind } from './types.ts';
|
|
2
|
+
export { ANIM_STATES, BALL_MODES, CHARACTER_IDS, NO_INPUT, other, PHASES, POWERUP_KINDS, SPECIAL_KINDS } from './types.ts';
|
|
3
|
+
export * from './constants.ts';
|
|
4
|
+
export type { Rng } from './rng.ts';
|
|
5
|
+
export { createRng } from './rng.ts';
|
|
6
|
+
export { decodeInput, encodeInput, INPUT_BITS, INPUT_MASK } from './input.ts';
|
|
7
|
+
export { CHARACTER_HEADS, CHARACTER_SPECIALS, CHARACTER_UNLOCK_AFTER, isCharacterId, specialOf, STARTER_CHARACTERS } from './characters.ts';
|
|
8
|
+
export type { BounceSurface, SimEvent, SimEventType, WhistleReason } from './events.ts';
|
|
9
|
+
export { SIM_EVENT_TYPES } from './events.ts';
|
|
10
|
+
export type { ActiveEffect, BallState, GoalState, MatchOptions, MatchState, PlayerState, PowerUpItem } from './state.ts';
|
|
11
|
+
export type { Circle, Rect } from './geometry.ts';
|
|
12
|
+
export { bodyCircle, chestCircle, crossbarRect, footCircle, headCircle, shieldRect } from './geometry.ts';
|
|
13
|
+
export { cloneMatch, clockSeconds, createMatch, goalEntered, isOver, stepMatch } from './match.ts';
|
|
14
|
+
export { animOf, animTime } from './anim.ts';
|
|
15
|
+
export { effectDuration, effectOn, effectTarget, hasEffect } from './powerups.ts';
|
|
16
|
+
export { energyFull } from './specials.ts';
|
|
17
|
+
export { botInput } from './bot.ts';
|
|
18
|
+
export { debugFillEnergy, debugSetMatchTime, debugSpawnPowerUp } from './debug.ts';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { ANIM_STATES, BALL_MODES, CHARACTER_IDS, NO_INPUT, other, PHASES, POWERUP_KINDS, SPECIAL_KINDS } from "./types.js";
|
|
2
|
+
export * from "./constants.js";
|
|
3
|
+
export { createRng } from "./rng.js";
|
|
4
|
+
export { decodeInput, encodeInput, INPUT_BITS, INPUT_MASK } from "./input.js";
|
|
5
|
+
export { CHARACTER_HEADS, CHARACTER_SPECIALS, CHARACTER_UNLOCK_AFTER, isCharacterId, specialOf, STARTER_CHARACTERS } from "./characters.js";
|
|
6
|
+
export { SIM_EVENT_TYPES } from "./events.js";
|
|
7
|
+
export { bodyCircle, chestCircle, crossbarRect, footCircle, headCircle, shieldRect } from "./geometry.js";
|
|
8
|
+
export { cloneMatch, clockSeconds, createMatch, goalEntered, isOver, stepMatch } from "./match.js";
|
|
9
|
+
export { animOf, animTime } from "./anim.js";
|
|
10
|
+
export { effectDuration, effectOn, effectTarget, hasEffect } from "./powerups.js";
|
|
11
|
+
export { energyFull } from "./specials.js";
|
|
12
|
+
export { botInput } from "./bot.js";
|
|
13
|
+
export { debugFillEnergy, debugSetMatchTime, debugSpawnPowerUp } from "./debug.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { InputFrame } from './types.ts';
|
|
2
|
+
export declare const INPUT_BITS: {
|
|
3
|
+
readonly left: 1;
|
|
4
|
+
readonly right: 2;
|
|
5
|
+
readonly jump: 4;
|
|
6
|
+
readonly kick: 8;
|
|
7
|
+
readonly special: 16;
|
|
8
|
+
};
|
|
9
|
+
/** Every bit of an input mask. */
|
|
10
|
+
export declare const INPUT_MASK = 31;
|
|
11
|
+
export declare function encodeInput(frame: Readonly<InputFrame>): number;
|
|
12
|
+
export declare function decodeInput(bits: number): InputFrame;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const INPUT_BITS = {
|
|
2
|
+
left: 1,
|
|
3
|
+
right: 2,
|
|
4
|
+
jump: 4,
|
|
5
|
+
kick: 8,
|
|
6
|
+
special: 16
|
|
7
|
+
};
|
|
8
|
+
/** Every bit of an input mask. */
|
|
9
|
+
export const INPUT_MASK = 31;
|
|
10
|
+
export function encodeInput(frame) {
|
|
11
|
+
return (frame.left ? INPUT_BITS.left : 0)
|
|
12
|
+
| (frame.right ? INPUT_BITS.right : 0)
|
|
13
|
+
| (frame.jump ? INPUT_BITS.jump : 0)
|
|
14
|
+
| (frame.kick ? INPUT_BITS.kick : 0)
|
|
15
|
+
| (frame.special ? INPUT_BITS.special : 0);
|
|
16
|
+
}
|
|
17
|
+
export function decodeInput(bits) {
|
|
18
|
+
return {
|
|
19
|
+
left: (bits & INPUT_BITS.left) !== 0,
|
|
20
|
+
right: (bits & INPUT_BITS.right) !== 0,
|
|
21
|
+
jump: (bits & INPUT_BITS.jump) !== 0,
|
|
22
|
+
kick: (bits & INPUT_BITS.kick) !== 0,
|
|
23
|
+
special: (bits & INPUT_BITS.special) !== 0
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SimEvent } from './events.ts';
|
|
2
|
+
import type { MatchOptions, MatchState } from './state.ts';
|
|
3
|
+
import type { InputFrame, Side } from './types.ts';
|
|
4
|
+
/** A new match, waiting for its first kickoff. */
|
|
5
|
+
export declare function createMatch(options: MatchOptions): MatchState;
|
|
6
|
+
/** An independent copy of `state`, for replays and what-ifs. */
|
|
7
|
+
export declare function cloneMatch(state: Readonly<MatchState>): MatchState;
|
|
8
|
+
export declare const isOver: (state: Readonly<MatchState>) => boolean;
|
|
9
|
+
/** Seconds of regular time left, rounded up for a clock display. */
|
|
10
|
+
export declare function clockSeconds(state: Readonly<MatchState>): number;
|
|
11
|
+
/** The goal (0 left, 1 right) the ball is fully inside, or null. */
|
|
12
|
+
export declare function goalEntered(state: Readonly<MatchState>): Side | null;
|
|
13
|
+
/**
|
|
14
|
+
* Advances the match one tick with both players' inputs and returns what happened on it. It changes `state` in place.
|
|
15
|
+
* A finished match stays as it is, apart from the players' animation clocks.
|
|
16
|
+
*/
|
|
17
|
+
export declare function stepMatch(state: MatchState, inputs: readonly [Readonly<InputFrame>, Readonly<InputFrame>]): SimEvent[];
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { updateAnims } from "./anim.js";
|
|
2
|
+
import { clearMode, newBall, resetBall, updateBall } from "./ball.js";
|
|
3
|
+
import { isCharacterId } from "./characters.js";
|
|
4
|
+
import { CROSSBAR_Y, CROSSBAR_THICKNESS, DT, ENERGY_PER_SECOND, GOAL_DEPTH, GOAL_PAUSE_TICKS, GOLDEN_GOAL_MAX_TICKS, KICKOFF_TICKS, MATCH_SECONDS, POWERUP_SPAWN_MAX_TICKS, POWERUP_SPAWN_MIN_TICKS, secondsToTicks, WORLD_W } from "./constants.js";
|
|
5
|
+
import { newPlayer, resetPlayer, separatePlayers, updatePlayer } from "./player.js";
|
|
6
|
+
import { consumeEffect, endEffects, hasEffect, refreshDerived, tickEffects, tickSpawner, updateItems } from "./powerups.js";
|
|
7
|
+
import { createRng } from "./rng.js";
|
|
8
|
+
import { addEnergy } from "./specials.js";
|
|
9
|
+
import { other } from "./types.js";
|
|
10
|
+
/** A new match, waiting for its first kickoff. */
|
|
11
|
+
export function createMatch(options) {
|
|
12
|
+
const { seed, characters } = options;
|
|
13
|
+
if (!Number.isFinite(seed)) {
|
|
14
|
+
throw new RangeError(`createMatch: seed must be finite, got ${seed}`);
|
|
15
|
+
}
|
|
16
|
+
for (const character of characters) {
|
|
17
|
+
if (!isCharacterId(character)) {
|
|
18
|
+
throw new RangeError(`createMatch: unknown character ${String(character)}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const seconds = options.matchSeconds ?? MATCH_SECONDS;
|
|
22
|
+
if (!Number.isFinite(seconds) || seconds <= 0) {
|
|
23
|
+
throw new RangeError(`createMatch: matchSeconds must be positive, got ${seconds}`);
|
|
24
|
+
}
|
|
25
|
+
const rng = createRng(seed);
|
|
26
|
+
const state = {
|
|
27
|
+
tick: 0,
|
|
28
|
+
phase: 'kickoff',
|
|
29
|
+
phaseTicks: KICKOFF_TICKS,
|
|
30
|
+
clockTicks: secondsToTicks(seconds),
|
|
31
|
+
goldenGoal: false,
|
|
32
|
+
goldenTicks: 0,
|
|
33
|
+
score: [0, 0],
|
|
34
|
+
players: [newPlayer(0, characters[0]), newPlayer(1, characters[1])],
|
|
35
|
+
ball: newBall(),
|
|
36
|
+
goals: [
|
|
37
|
+
{ crossbarY: CROSSBAR_Y, depth: GOAL_DEPTH, shield: false },
|
|
38
|
+
{ crossbarY: CROSSBAR_Y, depth: GOAL_DEPTH, shield: false }
|
|
39
|
+
],
|
|
40
|
+
items: [],
|
|
41
|
+
effects: [],
|
|
42
|
+
nextItemTicks: rng.int(POWERUP_SPAWN_MIN_TICKS, POWERUP_SPAWN_MAX_TICKS),
|
|
43
|
+
nextItemId: 1,
|
|
44
|
+
powerUps: options.powerUps ?? true,
|
|
45
|
+
lastScorer: null,
|
|
46
|
+
winner: null,
|
|
47
|
+
rng: 0
|
|
48
|
+
};
|
|
49
|
+
state.rng = rng.state();
|
|
50
|
+
return state;
|
|
51
|
+
}
|
|
52
|
+
/** An independent copy of `state`, for replays and what-ifs. */
|
|
53
|
+
export function cloneMatch(state) {
|
|
54
|
+
return structuredClone(state);
|
|
55
|
+
}
|
|
56
|
+
export const isOver = (state) => state.phase === 'over';
|
|
57
|
+
/** Seconds of regular time left, rounded up for a clock display. */
|
|
58
|
+
export function clockSeconds(state) {
|
|
59
|
+
return Math.ceil(state.clockTicks / secondsToTicks(1));
|
|
60
|
+
}
|
|
61
|
+
/** The goal (0 left, 1 right) the ball is fully inside, or null. */
|
|
62
|
+
export function goalEntered(state) {
|
|
63
|
+
const { ball, goals } = state;
|
|
64
|
+
for (const side of [0, 1]) {
|
|
65
|
+
const goal = goals[side];
|
|
66
|
+
const pastLine = side === 0 ? ball.x <= goal.depth - ball.r : ball.x >= WORLD_W - goal.depth + ball.r;
|
|
67
|
+
if (pastLine && ball.y > goal.crossbarY + CROSSBAR_THICKNESS) {
|
|
68
|
+
return side;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
function scoreGoal(state, goal, events) {
|
|
74
|
+
const scorer = other(goal);
|
|
75
|
+
let points = 1;
|
|
76
|
+
if (hasEffect(state, 'doubleGoal', scorer)) {
|
|
77
|
+
points = 2;
|
|
78
|
+
consumeEffect(state, 'doubleGoal', scorer, events);
|
|
79
|
+
}
|
|
80
|
+
state.score[scorer] += points;
|
|
81
|
+
state.lastScorer = scorer;
|
|
82
|
+
const golden = state.goldenGoal;
|
|
83
|
+
if (golden) {
|
|
84
|
+
state.winner = scorer;
|
|
85
|
+
}
|
|
86
|
+
state.phase = 'goal';
|
|
87
|
+
state.phaseTicks = GOAL_PAUSE_TICKS;
|
|
88
|
+
// The ball that scored is a plain ball now: no shot acts during the pause.
|
|
89
|
+
clearMode(state.ball);
|
|
90
|
+
// Nobody acts on the ball during the pause: a kick already swinging would fire an armed special or spend a fire
|
|
91
|
+
// shot, and a titan charge would run on through the restart.
|
|
92
|
+
for (const p of state.players) {
|
|
93
|
+
p.kickTicks = 0;
|
|
94
|
+
p.kickHit = true;
|
|
95
|
+
p.dashTicks = 0;
|
|
96
|
+
p.vx = 0;
|
|
97
|
+
}
|
|
98
|
+
events.push({
|
|
99
|
+
type: 'goal',
|
|
100
|
+
scorer,
|
|
101
|
+
points,
|
|
102
|
+
ownGoal: state.ball.lastToucher === goal,
|
|
103
|
+
score: [state.score[0], state.score[1]],
|
|
104
|
+
golden
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function finish(state, winner, events) {
|
|
108
|
+
state.phase = 'over';
|
|
109
|
+
state.phaseTicks = 0;
|
|
110
|
+
state.winner = winner;
|
|
111
|
+
events.push({ type: 'whistle', reason: 'end' });
|
|
112
|
+
}
|
|
113
|
+
function startKickoff(state, events) {
|
|
114
|
+
state.phase = 'kickoff';
|
|
115
|
+
state.phaseTicks = KICKOFF_TICKS;
|
|
116
|
+
// Nobody starts a restart frozen; the other timed effects carry on.
|
|
117
|
+
endEffects(state, 'freezeFoe', events);
|
|
118
|
+
for (const p of state.players) {
|
|
119
|
+
resetPlayer(p);
|
|
120
|
+
}
|
|
121
|
+
resetBall(state.ball);
|
|
122
|
+
refreshDerived(state);
|
|
123
|
+
}
|
|
124
|
+
/** Regular time runs down during play; level at the end means golden goal, which itself ends as a draw in time. */
|
|
125
|
+
function tickClock(state, events) {
|
|
126
|
+
if (state.goldenGoal) {
|
|
127
|
+
state.goldenTicks++;
|
|
128
|
+
if (state.goldenTicks >= GOLDEN_GOAL_MAX_TICKS) {
|
|
129
|
+
finish(state, null, events);
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
state.clockTicks = Math.max(0, state.clockTicks - 1);
|
|
134
|
+
if (state.clockTicks > 0) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const [left, right] = state.score;
|
|
138
|
+
if (left !== right) {
|
|
139
|
+
finish(state, left > right ? 0 : 1, events);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
state.goldenGoal = true;
|
|
143
|
+
events.push({ type: 'whistle', reason: 'goldenGoal' });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Advances the match one tick with both players' inputs and returns what happened on it. It changes `state` in place.
|
|
148
|
+
* A finished match stays as it is, apart from the players' animation clocks.
|
|
149
|
+
*/
|
|
150
|
+
export function stepMatch(state, inputs) {
|
|
151
|
+
const events = [];
|
|
152
|
+
const rng = createRng(state.rng);
|
|
153
|
+
state.tick++;
|
|
154
|
+
switch (state.phase) {
|
|
155
|
+
case 'kickoff':
|
|
156
|
+
// Everyone waits in place; the previous tick's inputs are still recorded, so a held key is not a fresh press.
|
|
157
|
+
for (const side of [0, 1]) {
|
|
158
|
+
updatePlayer(state, side, inputs[side], events);
|
|
159
|
+
}
|
|
160
|
+
state.phaseTicks--;
|
|
161
|
+
if (state.phaseTicks <= 0) {
|
|
162
|
+
state.phase = 'play';
|
|
163
|
+
state.phaseTicks = 0;
|
|
164
|
+
events.push({ type: 'whistle', reason: 'kickoff' });
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
case 'play': {
|
|
168
|
+
updatePlayer(state, 0, inputs[0], events);
|
|
169
|
+
updatePlayer(state, 1, inputs[1], events);
|
|
170
|
+
separatePlayers(state);
|
|
171
|
+
updateBall(state, events);
|
|
172
|
+
updateItems(state, events);
|
|
173
|
+
const goal = goalEntered(state);
|
|
174
|
+
if (goal !== null) {
|
|
175
|
+
scoreGoal(state, goal, events);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
for (const p of state.players) {
|
|
179
|
+
addEnergy(p, ENERGY_PER_SECOND * DT);
|
|
180
|
+
}
|
|
181
|
+
tickSpawner(state, rng, events);
|
|
182
|
+
tickEffects(state, events);
|
|
183
|
+
tickClock(state, events);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case 'goal':
|
|
187
|
+
updatePlayer(state, 0, inputs[0], events);
|
|
188
|
+
updatePlayer(state, 1, inputs[1], events);
|
|
189
|
+
separatePlayers(state);
|
|
190
|
+
updateBall(state, events);
|
|
191
|
+
state.phaseTicks--;
|
|
192
|
+
if (state.phaseTicks <= 0) {
|
|
193
|
+
if (state.winner !== null) {
|
|
194
|
+
finish(state, state.winner, events);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
startKickoff(state, events);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case 'over':
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
refreshDerived(state);
|
|
205
|
+
updateAnims(state);
|
|
206
|
+
state.rng = rng.state();
|
|
207
|
+
return events;
|
|
208
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SimEvent } from './events.ts';
|
|
2
|
+
import type { MatchState, PlayerState } from './state.ts';
|
|
3
|
+
import type { CharacterId, InputFrame, Side } from './types.ts';
|
|
4
|
+
export declare function newPlayer(side: Side, character: CharacterId): PlayerState;
|
|
5
|
+
/** Back to the starting spot for a kickoff, with every move in progress cut short. Energy and an armed special stay. */
|
|
6
|
+
export declare function resetPlayer(p: PlayerState): void;
|
|
7
|
+
/** Whether `p` obeys its controls this tick. */
|
|
8
|
+
export declare function inControl(state: Readonly<MatchState>, p: Readonly<PlayerState>): boolean;
|
|
9
|
+
/** Reads `frame` (with presses against the previous tick), then moves `p` one tick. */
|
|
10
|
+
export declare function updatePlayer(state: MatchState, side: Side, frame: Readonly<InputFrame>, events: SimEvent[]): void;
|
|
11
|
+
/**
|
|
12
|
+
* Two players never overlap: they are pushed apart along x until neither their bodies nor their heads overlap, and
|
|
13
|
+
* further when the ball is between them, so it is never wedged where it cannot move. A titan charge running into the
|
|
14
|
+
* opponent knocks them back once per charge.
|
|
15
|
+
*/
|
|
16
|
+
export declare function separatePlayers(state: MatchState): void;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { AIR_ACCEL, AIR_DECEL, DASH_SPEED, DT, GROUND_ACCEL, GROUND_DECEL, GROUND_Y, HEAD_RADIUS, JUMP_SPEED, KICK_TICKS, PLAYER_GRAVITY, PLAYER_MAX_X, PLAYER_MIN_X, PLAYER_SEPARATION, PLAYER_START_X, RUN_SPEED, SPEED_BOOST_JUMP, SPEED_BOOST_MOVE } from "./constants.js";
|
|
2
|
+
import { bodyCircle, chestCircle, crossbarRect, footCircle, headCircle } from "./geometry.js";
|
|
3
|
+
import { encodeInput, INPUT_BITS } from "./input.js";
|
|
4
|
+
import { hasEffect } from "./powerups.js";
|
|
5
|
+
import { knockBack, pressSpecial } from "./specials.js";
|
|
6
|
+
export function newPlayer(side, character) {
|
|
7
|
+
return {
|
|
8
|
+
side,
|
|
9
|
+
character,
|
|
10
|
+
x: PLAYER_START_X[side],
|
|
11
|
+
y: GROUND_Y,
|
|
12
|
+
vx: 0,
|
|
13
|
+
vy: 0,
|
|
14
|
+
onGround: true,
|
|
15
|
+
facing: side === 0 ? 1 : -1,
|
|
16
|
+
headScale: 1,
|
|
17
|
+
frozen: false,
|
|
18
|
+
energy: 0,
|
|
19
|
+
kickTicks: 0,
|
|
20
|
+
kickHit: false,
|
|
21
|
+
headerTicks: 0,
|
|
22
|
+
specialTicks: 0,
|
|
23
|
+
specialArmed: false,
|
|
24
|
+
dashTicks: 0,
|
|
25
|
+
dashHitBall: false,
|
|
26
|
+
dashHitFoe: false,
|
|
27
|
+
stunTicks: 0,
|
|
28
|
+
touchCooldown: 0,
|
|
29
|
+
input: 0,
|
|
30
|
+
anim: 'idle',
|
|
31
|
+
animTicks: 0
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Back to the starting spot for a kickoff, with every move in progress cut short. Energy and an armed special stay. */
|
|
35
|
+
export function resetPlayer(p) {
|
|
36
|
+
p.x = PLAYER_START_X[p.side];
|
|
37
|
+
p.y = GROUND_Y;
|
|
38
|
+
p.vx = 0;
|
|
39
|
+
p.vy = 0;
|
|
40
|
+
p.onGround = true;
|
|
41
|
+
p.kickTicks = 0;
|
|
42
|
+
p.kickHit = false;
|
|
43
|
+
p.headerTicks = 0;
|
|
44
|
+
p.specialTicks = 0;
|
|
45
|
+
p.dashTicks = 0;
|
|
46
|
+
p.stunTicks = 0;
|
|
47
|
+
}
|
|
48
|
+
/** Whether `p` obeys its controls this tick. */
|
|
49
|
+
export function inControl(state, p) {
|
|
50
|
+
return state.phase === 'play' && !p.frozen && p.stunTicks === 0 && p.dashTicks === 0;
|
|
51
|
+
}
|
|
52
|
+
const approach = (value, target, step) => value < target ? Math.min(target, value + step) : Math.max(target, value - step);
|
|
53
|
+
/** Reads `frame` (with presses against the previous tick), then moves `p` one tick. */
|
|
54
|
+
export function updatePlayer(state, side, frame, events) {
|
|
55
|
+
const p = state.players[side];
|
|
56
|
+
const bits = encodeInput(frame);
|
|
57
|
+
const pressed = bits & ~p.input;
|
|
58
|
+
p.input = bits;
|
|
59
|
+
const control = inControl(state, p);
|
|
60
|
+
const boosted = hasEffect(state, 'speedBoost', side);
|
|
61
|
+
if (p.dashTicks > 0) {
|
|
62
|
+
p.vx = p.facing * DASH_SPEED;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
let dir = control ? (frame.right ? 1 : 0) - (frame.left ? 1 : 0) : 0;
|
|
66
|
+
if (hasEffect(state, 'reverseFoe', side)) {
|
|
67
|
+
dir = -dir;
|
|
68
|
+
}
|
|
69
|
+
const target = dir * RUN_SPEED * (boosted ? SPEED_BOOST_MOVE : 1);
|
|
70
|
+
const rate = dir === 0 ? (p.onGround ? GROUND_DECEL : AIR_DECEL) : (p.onGround ? GROUND_ACCEL : AIR_ACCEL);
|
|
71
|
+
// A knocked-back player slides to a stop instead of braking at once.
|
|
72
|
+
p.vx = approach(p.vx, target, rate * DT * (p.stunTicks > 0 ? 0.3 : 1));
|
|
73
|
+
}
|
|
74
|
+
if (control && frame.jump && p.onGround) {
|
|
75
|
+
p.vy = -JUMP_SPEED * (boosted ? SPEED_BOOST_JUMP : 1);
|
|
76
|
+
p.onGround = false;
|
|
77
|
+
}
|
|
78
|
+
if (control && (pressed & INPUT_BITS.kick) !== 0 && p.kickTicks === 0) {
|
|
79
|
+
p.kickTicks = KICK_TICKS;
|
|
80
|
+
p.kickHit = false;
|
|
81
|
+
}
|
|
82
|
+
if (control && (pressed & INPUT_BITS.special) !== 0) {
|
|
83
|
+
pressSpecial(state, side, events);
|
|
84
|
+
}
|
|
85
|
+
p.vy += PLAYER_GRAVITY * DT;
|
|
86
|
+
p.x += p.vx * DT;
|
|
87
|
+
p.y += p.vy * DT;
|
|
88
|
+
if (p.y >= GROUND_Y) {
|
|
89
|
+
p.y = GROUND_Y;
|
|
90
|
+
p.vy = 0;
|
|
91
|
+
p.onGround = true;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
p.onGround = false;
|
|
95
|
+
}
|
|
96
|
+
keepInField(p);
|
|
97
|
+
keepUnderCrossbars(state, p);
|
|
98
|
+
p.kickTicks = Math.max(0, p.kickTicks - 1);
|
|
99
|
+
p.headerTicks = Math.max(0, p.headerTicks - 1);
|
|
100
|
+
p.specialTicks = Math.max(0, p.specialTicks - 1);
|
|
101
|
+
p.stunTicks = Math.max(0, p.stunTicks - 1);
|
|
102
|
+
p.touchCooldown = Math.max(0, p.touchCooldown - 1);
|
|
103
|
+
if (p.dashTicks > 0) {
|
|
104
|
+
p.dashTicks--;
|
|
105
|
+
if (p.dashTicks === 0) {
|
|
106
|
+
p.vx = 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function keepInField(p) {
|
|
111
|
+
if (p.x < PLAYER_MIN_X) {
|
|
112
|
+
p.x = PLAYER_MIN_X;
|
|
113
|
+
p.vx = Math.max(0, p.vx);
|
|
114
|
+
}
|
|
115
|
+
else if (p.x > PLAYER_MAX_X) {
|
|
116
|
+
p.x = PLAYER_MAX_X;
|
|
117
|
+
p.vx = Math.min(0, p.vx);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A player inside a goal mouth bumps their head on the crossbar. One who cannot stand under it (a lowered crossbar) or
|
|
122
|
+
* whose head came up beside it is pushed back out in front of the goal.
|
|
123
|
+
*/
|
|
124
|
+
function keepUnderCrossbars(state, p) {
|
|
125
|
+
for (const side of [0, 1]) {
|
|
126
|
+
const bar = crossbarRect(state.goals[side], side);
|
|
127
|
+
const head = headCircle(p);
|
|
128
|
+
if (head.x + head.r <= bar.left || head.x - head.r >= bar.right) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const outside = side === 0 ? bar.right + head.r : bar.left - head.r;
|
|
132
|
+
if (head.y >= bar.bottom) {
|
|
133
|
+
if (head.y - head.r < bar.bottom) {
|
|
134
|
+
// Stand the player where the top of the head just touches the bar.
|
|
135
|
+
const feet = bar.bottom + (p.y - head.y) + head.r;
|
|
136
|
+
if (feet <= GROUND_Y) {
|
|
137
|
+
p.y = feet;
|
|
138
|
+
p.vy = Math.max(0, p.vy);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
p.x = outside;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (p.y > bar.top) {
|
|
146
|
+
p.x = outside;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* How far from `p`'s centre line a ball at height `y` touches `p` on the side toward `dir`: the widest of its head,
|
|
152
|
+
* body and foot circles grown by the ball's radius at that height, or 0 if the ball passes above or below.
|
|
153
|
+
*/
|
|
154
|
+
function reachAt(p, ball, dir) {
|
|
155
|
+
let reach = 0;
|
|
156
|
+
for (const c of [headCircle(p), chestCircle(p), bodyCircle(p), footCircle(p)]) {
|
|
157
|
+
const dy = ball.y - c.y;
|
|
158
|
+
const r = c.r + ball.r;
|
|
159
|
+
if (Math.abs(dy) < r) {
|
|
160
|
+
reach = Math.max(reach, (c.x - p.x) * dir + Math.sqrt(r * r - dy * dy));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return reach;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Two players never overlap: they are pushed apart along x until neither their bodies nor their heads overlap, and
|
|
167
|
+
* further when the ball is between them, so it is never wedged where it cannot move. A titan charge running into the
|
|
168
|
+
* opponent knocks them back once per charge.
|
|
169
|
+
*/
|
|
170
|
+
export function separatePlayers(state) {
|
|
171
|
+
const [a, b] = state.players;
|
|
172
|
+
if (Math.abs(a.y - b.y) >= 150) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const dx = b.x - a.x;
|
|
176
|
+
// Player 0 is pushed left when both stand at the same x.
|
|
177
|
+
const dir = dx === 0 ? 1 : Math.sign(dx);
|
|
178
|
+
const [left, right] = dir > 0 ? [a, b] : [b, a];
|
|
179
|
+
const bodies = Math.max(PLAYER_SEPARATION, HEAD_RADIUS * (a.headScale + b.headScale) + 4);
|
|
180
|
+
if (Math.abs(dx) < bodies) {
|
|
181
|
+
for (const [charger, foe] of [[a, b], [b, a]]) {
|
|
182
|
+
if (charger.dashTicks > 0 && !charger.dashHitFoe) {
|
|
183
|
+
charger.dashHitFoe = true;
|
|
184
|
+
knockBack(foe, charger.x);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const ball = state.ball;
|
|
189
|
+
const between = ball.x > left.x && ball.x < right.x;
|
|
190
|
+
const room = between ? reachAt(left, ball, 1) + reachAt(right, ball, -1) + 2 : 0;
|
|
191
|
+
const gap = Math.max(bodies, room);
|
|
192
|
+
if (right.x - left.x >= gap) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const push = (gap - (right.x - left.x)) / 2;
|
|
196
|
+
left.x = Math.max(PLAYER_MIN_X, left.x - push);
|
|
197
|
+
right.x = Math.min(PLAYER_MAX_X, right.x + push);
|
|
198
|
+
// Pinned against a wall, the other player takes the whole push.
|
|
199
|
+
if (right.x - left.x < gap) {
|
|
200
|
+
if (left.x <= PLAYER_MIN_X) {
|
|
201
|
+
right.x = Math.min(PLAYER_MAX_X, left.x + gap);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
left.x = Math.max(PLAYER_MIN_X, right.x - gap);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SimEvent } from './events.ts';
|
|
2
|
+
import type { Rng } from './rng.ts';
|
|
3
|
+
import type { ActiveEffect, MatchState, PowerUpItem } from './state.ts';
|
|
4
|
+
import type { PowerUpKind, Side } from './types.ts';
|
|
5
|
+
/** The player an effect of `kind` collected by `owner` acts on. Ball effects are kept on their collector. */
|
|
6
|
+
export declare function effectTarget(kind: PowerUpKind, owner: Side): Side;
|
|
7
|
+
export declare function effectDuration(kind: PowerUpKind): number;
|
|
8
|
+
export declare function effectOn(state: Readonly<MatchState>, kind: PowerUpKind, target: Side): ActiveEffect | undefined;
|
|
9
|
+
export declare function hasEffect(state: Readonly<MatchState>, kind: PowerUpKind, target: Side): boolean;
|
|
10
|
+
/** Whether any effect of `kind` is at work, whoever it acts on. */
|
|
11
|
+
export declare function anyEffect(state: Readonly<MatchState>, kind: PowerUpKind): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Puts an effect of `kind` collected by `owner` to work on `target` for `ticks`. The same kind on the same target
|
|
14
|
+
* starts its timer again; a rival of its group (big head against shrink, big goal against small goal, big ball against
|
|
15
|
+
* small ball) is replaced.
|
|
16
|
+
*/
|
|
17
|
+
export declare function addEffect(state: MatchState, kind: PowerUpKind, owner: Side, target: Side, ticks: number, events: SimEvent[]): void;
|
|
18
|
+
/** `owner` collected a power-up of `kind`. */
|
|
19
|
+
export declare function applyPowerUp(state: MatchState, kind: PowerUpKind, owner: Side, events: SimEvent[]): void;
|
|
20
|
+
/** Ends the effect of `kind` on `target` early, because it was used up (a fire shot kicked, a double goal scored). */
|
|
21
|
+
export declare function consumeEffect(state: MatchState, kind: PowerUpKind, target: Side, events: SimEvent[]): void;
|
|
22
|
+
/** Ends every effect of `kind` on either player, as if each ran out now. */
|
|
23
|
+
export declare function endEffects(state: MatchState, kind: PowerUpKind, events: SimEvent[]): void;
|
|
24
|
+
/** One tick of every effect's timer; effects that ran out end. */
|
|
25
|
+
export declare function tickEffects(state: MatchState, events: SimEvent[]): void;
|
|
26
|
+
/** Recomputes everything the effects decide: head sizes, frozen players, goal sizes, shields and the ball's radius. */
|
|
27
|
+
export declare function refreshDerived(state: MatchState): void;
|
|
28
|
+
/** Adds an item of `kind` at (x, y); it falls until it rests on the grass. */
|
|
29
|
+
export declare function spawnItem(state: MatchState, kind: PowerUpKind, x: number, y?: number): PowerUpItem;
|
|
30
|
+
/** One tick of the spawn timer: an item of a random kind drops at a random x every 6 to 9 s, at most two at once. */
|
|
31
|
+
export declare function tickSpawner(state: MatchState, rng: Rng, events: SimEvent[]): void;
|
|
32
|
+
/** Items fall, age and vanish, and are collected. */
|
|
33
|
+
export declare function updateItems(state: MatchState, events: SimEvent[]): void;
|