Skip to content

Commit 534846a

Browse files
committed
feat: player accel and decel
1 parent bf3a1df commit 534846a

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

src/constants.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
// 単位は blockSize * px
1+
/// 単位は blockSize * px
2+
// global
3+
export const gravity = 0.02;
4+
// Player 関連
25
export const playerWidth = 0.6;
36
export const playerHeight = 0.9;
4-
export const moveVX = 0.1;
7+
export const maxMoveVX = 0.1;
8+
export const playerAccelOnGround = 0.05;
9+
export const playerDecelOnGround = 0.05;
10+
export const playerAccelInAir = 0.01;
11+
export const playerDecelInAir = 0.01;
512
export const jumpVY = 0.12;
613
export const jumpFrames = 10;
7-
export const gravity = 0.02;
14+
// Fallable block 関連
815
export const maxObjectFallSpeed = 2;
916

1017
export enum Block {

src/main.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function setup(
2020
uiInfo: UIInfo;
2121
},
2222
): Promise<void> {
23-
const cleanups: (() => void)[] = [];
23+
const destroyer: (() => void)[] = [];
2424
const unlessStopped = (f: (ticker: Ticker) => void) => (ticker: Ticker) => {
2525
const stopped = get(cx.state).paused || get(cx.state).goaled || get(cx.state).gameover;
2626
if (!stopped) {
@@ -45,7 +45,7 @@ export async function setup(
4545
const app = new Application();
4646
const stage = new Container();
4747
app.stage.addChild(stage);
48-
cleanups.push(() => {
48+
destroyer.push(() => {
4949
app.destroy(true, { children: true });
5050
});
5151

@@ -189,13 +189,15 @@ export async function setup(
189189
el.appendChild(app.canvas);
190190
const onresize = useOnResize(cx, app, grid, gridX, gridY);
191191
window.addEventListener("resize", onresize);
192-
cleanups.push(() => {
192+
destroyer.push(() => {
193193
window.removeEventListener("resize", onresize);
194194
});
195195

196196
bindings.destroy = () => {
197-
for (const cleanup of cleanups) {
198-
cleanup();
197+
for (const cleanup of destroyer) {
198+
try {
199+
cleanup();
200+
} catch {}
199201
}
200202
};
201203
bindings.resume = () => {

src/player.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,40 @@ export function handleInput(cx: Context, event: KeyboardEvent, eventIsKeyDown: b
128128
export function tick(cx: Context, ticker: Ticker) {
129129
const { blockSize, gridX, gridY, marginY } = get(cx.config);
130130
const player = cx.dynamic.player;
131-
player.vx = 0;
131+
const accel = player.onGround ? consts.playerAccelOnGround : consts.playerAccelInAir;
132+
const decel = player.onGround ? consts.playerDecelOnGround : consts.playerDecelInAir;
133+
let playerIntent = 0; // 0 -> no intent, 1 -> wants to go right, -1 -> wants to go left
134+
// positive direction
135+
if (player.holdingKeys[Inputs.Right]) {
136+
playerIntent = 1;
137+
}
132138
if (player.holdingKeys[Inputs.Left]) {
133-
player.vx -= consts.moveVX * blockSize;
139+
playerIntent = -1;
134140
}
135-
if (player.holdingKeys[Inputs.Right]) {
136-
player.vx += consts.moveVX * blockSize;
141+
switch (playerIntent) {
142+
case 1:
143+
player.vx += accel * blockSize;
144+
if (player.vx >= consts.maxMoveVX * blockSize) {
145+
player.vx = consts.maxMoveVX * blockSize;
146+
}
147+
break;
148+
case -1:
149+
player.vx -= decel * blockSize;
150+
if (player.vx <= -consts.maxMoveVX * blockSize) {
151+
player.vx = -consts.maxMoveVX * blockSize;
152+
}
153+
break;
154+
case 0:
155+
if (player.vx > decel * blockSize) {
156+
player.vx -= decel * blockSize;
157+
} else if (player.vx < -decel * blockSize) {
158+
player.vx += decel * blockSize;
159+
} else {
160+
player.vx = 0;
161+
}
162+
break;
163+
default:
164+
throw new Error(`[Player.tick] Invalid playerIntent, got ${playerIntent}`);
137165
}
138166
const elapsed = cx.elapsed;
139167
if (player.holdingKeys[Inputs.Up]) {

0 commit comments

Comments
 (0)