Skip to content

Commit 0e63917

Browse files
committed
fix: sharpen web canvas and gate input
1 parent d841365 commit 0e63917

4 files changed

Lines changed: 72 additions & 8 deletions

File tree

src/lib.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct SkiFree {
2323
status: GameStatus,
2424
gate_combo: u16,
2525
trick_combo: u16,
26+
input: InputRepeat,
2627
}
2728

2829
impl SkiFree {
@@ -38,6 +39,7 @@ impl SkiFree {
3839
status: GameStatus::Ready,
3940
gate_combo: 0,
4041
trick_combo: 0,
42+
input: InputRepeat::default(),
4143
}
4244
}
4345

@@ -48,6 +50,7 @@ impl SkiFree {
4850
self.status = GameStatus::Ready;
4951
self.gate_combo = 0;
5052
self.trick_combo = 0;
53+
self.input = InputRepeat::default();
5154
}
5255

5356
fn start(&mut self) {
@@ -71,18 +74,18 @@ impl SkiFree {
7174
self.start();
7275
}
7376

74-
if is_key_pressed(KeyCode::Left) {
77+
if self.input.consume(KeyCode::Left) {
7578
self.start();
7679
self.player.left();
7780
}
78-
if is_key_pressed(KeyCode::Right) {
81+
if self.input.consume(KeyCode::Right) {
7982
self.start();
8083
self.player.right();
8184
}
82-
if is_key_pressed(KeyCode::Up) {
85+
if is_key_down(KeyCode::Up) {
8386
self.player.brake();
8487
}
85-
if is_key_pressed(KeyCode::Down) {
88+
if is_key_down(KeyCode::Down) {
8689
self.start();
8790
self.player.down();
8891
}
@@ -239,6 +242,46 @@ enum GameStatus {
239242
Eaten,
240243
}
241244

245+
#[derive(Default)]
246+
struct InputRepeat {
247+
left: KeyRepeat,
248+
right: KeyRepeat,
249+
}
250+
251+
impl InputRepeat {
252+
fn consume(&mut self, key: KeyCode) -> bool {
253+
match key {
254+
KeyCode::Left => self.left.consume(key),
255+
KeyCode::Right => self.right.consume(key),
256+
_ => false,
257+
}
258+
}
259+
}
260+
261+
#[derive(Default)]
262+
struct KeyRepeat {
263+
held_frames: u8,
264+
}
265+
266+
impl KeyRepeat {
267+
const INITIAL_DELAY_FRAMES: u8 = 8;
268+
const REPEAT_FRAMES: u8 = 5;
269+
270+
fn consume(&mut self, key: KeyCode) -> bool {
271+
if !is_key_down(key) {
272+
self.held_frames = 0;
273+
return false;
274+
}
275+
276+
let should_fire = self.held_frames == 0
277+
|| (self.held_frames >= Self::INITIAL_DELAY_FRAMES
278+
&& (self.held_frames - Self::INITIAL_DELAY_FRAMES)
279+
.is_multiple_of(Self::REPEAT_FRAMES));
280+
self.held_frames = self.held_frames.saturating_add(1);
281+
should_fire
282+
}
283+
}
284+
242285
struct OverlayLine {
243286
text: &'static str,
244287
y_offset: f32,

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ fn window_conf() -> Conf {
77
window_height: skifree_rs::WINDOW_HEIGHT as i32,
88
window_resizable: false,
99
fullscreen: false,
10+
high_dpi: false,
11+
sample_count: 1,
1012
..Default::default()
1113
}
1214
}

src/map.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl Gate {
650650
camera: Camera,
651651
player: &Player,
652652
) -> Option<MapEvent> {
653-
if self.passed {
653+
if self.passed || !self.is_visible(camera) {
654654
return None;
655655
}
656656

@@ -675,6 +675,10 @@ impl Gate {
675675
}
676676

677677
fn check_collision(&mut self, camera: Camera, player: &Player) -> Option<CollisionAction> {
678+
if self.passed {
679+
return None;
680+
}
681+
678682
for object in [&mut self.left, &mut self.right] {
679683
if camera.object_hitbox(object).overlaps(&player.hitbox()) {
680684
return Some(object.collide());
@@ -684,6 +688,10 @@ impl Gate {
684688
}
685689

686690
fn draw(&self, camera: Camera) {
691+
if self.passed {
692+
return;
693+
}
694+
687695
for object in [&self.left, &self.right] {
688696
let position = camera.world_to_screen(object.position);
689697
if position.x >= 0. - 100.
@@ -697,6 +705,13 @@ impl Gate {
697705
}
698706
}
699707
}
708+
709+
fn is_visible(&self, camera: Camera) -> bool {
710+
[&self.left, &self.right].into_iter().any(|object| {
711+
let position = camera.world_to_screen(object.position);
712+
position.x >= 0. - 100. && position.x <= WINDOW_WIDTH + 100.
713+
})
714+
}
700715
}
701716

702717
struct FinishLine {

web/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
body {
1818
display: grid;
19+
overflow: auto;
1920
min-height: 100vh;
2021
place-items: center;
2122
}
2223

2324
canvas {
2425
display: block;
25-
width: min(480px, 100vw);
26-
height: min(640px, 100vh);
26+
width: 480px;
27+
height: 640px;
28+
aspect-ratio: 3 / 4;
29+
outline: none;
30+
image-rendering: crisp-edges;
2731
image-rendering: pixelated;
2832
}
2933

@@ -37,7 +41,7 @@
3741
</head>
3842
<body>
3943
<div id="status">Loading SkiFree...</div>
40-
<canvas id="glcanvas" tabindex="1"></canvas>
44+
<canvas id="glcanvas" width="480" height="640" tabindex="1"></canvas>
4145
<script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
4246
<script>
4347
startGame().catch(showError);

0 commit comments

Comments
 (0)