dotzuki-engine 0.1.0

A general-purpose JRPG game engine built from Game Boy tile rendering principles
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Generic player movement system for the overworld.
//!
//! Implements player movement: the movement loop with input handling,
//! walking/sprite-advance logic, and player state transitions.

use crate::map::MapTrait;
use crate::tileset::TilesetTrait;

use super::collision::{
    check_movement_collision, check_warp_at_position, is_facing_map_edge, CollisionProvider,
    CollisionResult, SpritePosition,
};
use super::types::{
    Direction, MapData, MovementState, OverworldState as GenericOverworldState, TransportMode,
};

/// Walk counter initial value (8 frames per tile).
pub const WALK_COUNTER_INIT: u8 = 8;

/// Input state from the player's controller.
#[derive(Debug, Clone, Copy, Default)]
pub struct InputState {
    pub up: bool,
    pub down: bool,
    pub left: bool,
    pub right: bool,
    pub a_button: bool,
    pub b_button: bool,
    pub start: bool,
    pub select: bool,
}

impl InputState {
    /// Get the direction being pressed, if any.
    /// Priority matches the original game: Down > Up > Left > Right.
    pub fn direction_pressed(&self) -> Option<Direction> {
        if self.down {
            Some(Direction::Down)
        } else if self.up {
            Some(Direction::Up)
        } else if self.left {
            Some(Direction::Left)
        } else if self.right {
            Some(Direction::Right)
        } else {
            None
        }
    }

    /// Convert to the raw d-pad bitmask used in the original game.
    pub fn to_pad_bits(&self) -> u8 {
        let mut bits = 0u8;
        if self.down {
            bits |= super::collision::PAD_DOWN;
        }
        if self.up {
            bits |= super::collision::PAD_UP;
        }
        if self.left {
            bits |= super::collision::PAD_LEFT;
        }
        if self.right {
            bits |= super::collision::PAD_RIGHT;
        }
        bits
    }
}

/// Result of processing a movement attempt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MoveResult {
    /// Player started walking to the target tile.
    Walking,
    /// Player started a ledge jump.
    LedgeJump,
    /// Player only turned to face a new direction (no movement).
    TurnedOnly,
    /// Movement was blocked (wall, NPC, etc.).
    Blocked(CollisionResult),
    /// Player reached the edge of the map (connection should be checked).
    ReachedMapEdge,
    /// Player stepped onto a warp tile.
    Warped { warp_index: usize },
    /// Player is still mid-step from previous movement.
    StillMoving,
    /// No input was pressed.
    NoInput,
}

/// Try to move the player in a direction.
///
/// Returns a `MoveResult` indicating what happened.
pub fn try_move<M: MapTrait, T: TilesetTrait>(
    state: &mut GenericOverworldState<M>,
    direction: Direction,
    tileset: T,
    map_width_blocks: u8,
    map_height_blocks: u8,
    standing_tile: u8,
    target_tile: u8,
    npc_positions: &[SpritePosition],
    held_input: u8,
    provider: &impl CollisionProvider<T>,
) -> MoveResult {
    if state.player.movement_state != MovementState::Idle {
        return MoveResult::StillMoving;
    }

    let was_facing = state.player.facing;
    state.player.facing = direction;

    let result = check_movement_collision(
        state.player.x,
        state.player.y,
        direction,
        tileset,
        map_width_blocks,
        map_height_blocks,
        standing_tile,
        target_tile,
        state.player.transport,
        npc_positions,
        held_input,
        provider,
    );

    match result {
        CollisionResult::Passable | CollisionResult::StopSurfing => {
            // StopSurfing: the surfer stepped onto a passable land tile and
            // returns to walking (CollisionCheckOnWater .stopSurfing).
            if result == CollisionResult::StopSurfing {
                state.player.transport = TransportMode::Walking;
            }
            state.standing_on_warp = false;
            state.player.movement_state = MovementState::Walking;
            state.walk_counter = WALK_COUNTER_INIT;
            MoveResult::Walking
        }
        CollisionResult::LedgeJump => {
            state.standing_on_warp = false;
            state.player.movement_state = MovementState::Jumping;
            state.walk_counter = WALK_COUNTER_INIT * 2;
            MoveResult::LedgeJump
        }
        CollisionResult::MapEdge => MoveResult::ReachedMapEdge,
        _ => {
            if was_facing != direction {
                MoveResult::TurnedOnly
            } else {
                MoveResult::Blocked(result)
            }
        }
    }
}

/// Advance the player's position by one pixel-step during movement.
///
/// Returns true when the step is complete (walk counter reached 0).
pub fn advance_step<M: MapTrait>(state: &mut GenericOverworldState<M>) -> bool {
    if state.walk_counter == 0 {
        return true;
    }

    let decrement = if state.player.transport == TransportMode::Biking
        && state.player.bike_speedup_active
    {
        2
    } else {
        1
    };

    state.walk_counter = state.walk_counter.saturating_sub(decrement);

    if state.walk_counter == 0 {
        let (dx, dy) = direction_delta(state.player.facing);

        if state.player.movement_state == MovementState::Jumping {
            let new_x = (state.player.x as i32 + dx as i32 * 2) as u16;
            let new_y = (state.player.y as i32 + dy as i32 * 2) as u16;
            state.player.x = new_x;
            state.player.y = new_y;
        } else {
            let new_x = (state.player.x as i32 + dx as i32) as u16;
            let new_y = (state.player.y as i32 + dy as i32) as u16;
            state.player.x = new_x;
            state.player.y = new_y;
        }

        state.player.movement_state = MovementState::Idle;

        if state.encounter_cooldown > 0 {
            state.encounter_cooldown -= 1;
        }

        if state.repel_steps > 0 {
            state.repel_steps -= 1;
        }

        return true;
    }

    false
}

/// Get the x/y delta for a direction.
pub fn direction_delta(dir: Direction) -> (i8, i8) {
    match dir {
        Direction::Down => (0, 1),
        Direction::Up => (0, -1),
        Direction::Left => (-1, 0),
        Direction::Right => (1, 0),
    }
}

/// Get the opposite direction.
pub fn opposite_direction(dir: Direction) -> Direction {
    match dir {
        Direction::Down => Direction::Up,
        Direction::Up => Direction::Down,
        Direction::Left => Direction::Right,
        Direction::Right => Direction::Left,
    }
}

/// Calculate the number of frames for a step based on transport mode.
pub fn frames_per_step(transport: TransportMode) -> u8 {
    match transport {
        TransportMode::Walking => WALK_COUNTER_INIT,
        TransportMode::Biking => WALK_COUNTER_INIT / 2,
        TransportMode::Surfing => WALK_COUNTER_INIT,
    }
}

/// Convert Direction to the facing index (0=Down, 1=Up, 2=Left, 3=Right).
pub fn direction_to_facing_index(dir: Direction) -> u8 {
    match dir {
        Direction::Down => 0,
        Direction::Up => 1,
        Direction::Left => 2,
        Direction::Right => 3,
    }
}

/// Get the tile ID at a specific position in the map.
pub fn get_tile_at_position<M: MapTrait, T: TilesetTrait, Mus>(
    map: &MapData<M, T, Mus>,
    x: u16,
    y: u16,
    provider: &impl CollisionProvider<T>,
) -> u8 {
    provider.get_tile_at_position(map.tileset, &map.blocks, map.width, x, y)
}

pub fn get_target_tile_for_direction<M: MapTrait, T: TilesetTrait, Mus>(
    map: &MapData<M, T, Mus>,
    x: u16,
    y: u16,
    dir: Direction,
    provider: &impl CollisionProvider<T>,
) -> u8 {
    let (dx, dy) = direction_delta(dir);
    let target_x = ((x as i32) + dx as i32).max(0) as u16;
    let target_y = ((y as i32) + dy as i32).max(0) as u16;
    provider.get_tile_at_position(map.tileset, &map.blocks, map.width, target_x, target_y)
}

/// Extra-warp check (classic behavior).
pub fn extra_warp_check<M: MapTrait, T: TilesetTrait, Mus>(
    map: &MapData<M, T, Mus>,
    player_x: u16,
    player_y: u16,
    facing: Direction,
    provider: &impl CollisionProvider<T>,
) -> bool {
    // Check for game-specific special cases (e.g. SS_ANNE_BOW tile 0x15).
    let tile_in_front = get_target_tile_for_direction(map, player_x, player_y, facing, provider);
    if let Some(result) = provider.check_extra_warp_special(map.tileset, tile_in_front) {
        return result;
    }

    if provider.uses_warp_tile_in_front_check(map.tileset) {
        let facing_idx = direction_to_facing_index(facing);
        provider.is_warp_carpet_tile_in_front(map.tileset, facing_idx, tile_in_front)
    } else {
        is_facing_map_edge(player_x, player_y, facing, map.width, map.height)
    }
}

/// Two-phase warp check after a step completes onto a warp position.
pub fn check_warps_no_collision<M: MapTrait, T: TilesetTrait, Mus>(
    state: &mut GenericOverworldState<M>,
    map: &MapData<M, T, Mus>,
    standing_tile: u8,
    direction_held: bool,
    provider: &impl CollisionProvider<T>,
) -> Option<usize> {
    let warp_idx = check_warp_at_position(state.player.x, state.player.y, map)?;

    state.standing_on_warp = true;

    if provider.is_door_tile(map.tileset, standing_tile) {
        return Some(warp_idx);
    }

    if provider.is_warp_tile(map.tileset, standing_tile) {
        state.standing_on_warp = false;
        return Some(warp_idx);
    }

    if extra_warp_check(map, state.player.x, state.player.y, state.player.facing, provider) {
        if direction_held {
            return Some(warp_idx);
        }
    }

    None
}

/// CheckWarpsCollision path: when collision occurs while standing_on_warp is set.
pub fn check_collision_warp<M: MapTrait, T: TilesetTrait, Mus>(
    state: &mut GenericOverworldState<M>,
    map: &MapData<M, T, Mus>,
    move_result: MoveResult,
    provider: &impl CollisionProvider<T>,
) -> MoveResult {
    match move_result {
        MoveResult::Blocked(_) | MoveResult::ReachedMapEdge => {
            if state.standing_on_warp {
                if extra_warp_check(
                    map,
                    state.player.x,
                    state.player.y,
                    state.player.facing,
                    provider,
                ) {
                    if let Some(warp_idx) =
                        check_warp_at_position(state.player.x, state.player.y, map)
                    {
                        return MoveResult::Warped {
                            warp_index: warp_idx,
                        };
                    }
                }
            }
            move_result
        }
        _ => move_result,
    }
}

/// Process one frame of overworld movement.
///
/// This is the high-level frame-by-frame update, combining input
/// processing and step advancement.
pub fn process_frame<M: MapTrait, T: TilesetTrait, Mus>(
    state: &mut GenericOverworldState<M>,
    input: &InputState,
    map: &MapData<M, T, Mus>,
    standing_tile: u8,
    target_tile: u8,
    npc_positions: &[SpritePosition],
    provider: &impl CollisionProvider<T>,
) -> MoveResult {
    // If currently moving, advance the step
    if state.player.movement_state != MovementState::Idle {
        let step_done = advance_step(state);
        if step_done {
            let new_standing_tile = get_tile_at_position(map, state.player.x, state.player.y, provider);
            let direction_held = input.direction_pressed().is_some();

            if let Some(warp_idx) = check_warps_no_collision(
                state,
                map,
                new_standing_tile,
                direction_held,
                provider,
            ) {
                return MoveResult::Warped {
                    warp_index: warp_idx,
                };
            }

            if let Some(direction) = input.direction_pressed() {
                let held_input = input.to_pad_bits();

                let new_target_tile = get_target_tile_for_direction(
                    map,
                    state.player.x,
                    state.player.y,
                    direction,
                    provider,
                );

                let move_result = try_move(
                    state,
                    direction,
                    map.tileset,
                    map.width,
                    map.height,
                    new_standing_tile,
                    new_target_tile,
                    npc_positions,
                    held_input,
                    provider,
                );

                return check_collision_warp(state, map, move_result, provider);
            }
        }
        return MoveResult::StillMoving;
    }

    // Not moving — check for new input
    let direction = match input.direction_pressed() {
        Some(dir) => dir,
        None => return MoveResult::NoInput,
    };

    let held_input = input.to_pad_bits();

    let move_result = try_move(
        state,
        direction,
        map.tileset,
        map.width,
        map.height,
        standing_tile,
        target_tile,
        npc_positions,
        held_input,
        provider,
    );

    check_collision_warp(state, map, move_result, provider)
}