suon_movement 0.1.0

Entity movement and grid navigation for the Suon MMORPG framework
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
//! Step-based movement systems.
//!
//! Step intents move an entity by one tile according to a [`Direction`], while
//! path advancement consumes queued directions from [`path::StepPath`] using a
//! per-entity [`timer::StepTimer`].

use crate::prelude::*;
use bevy::prelude::*;
use std::time::*;
use suon_chunk::{chunks::Chunks, occupancy::Occupancy};
use suon_position::{
    direction::Direction, floor::Floor, position::Position, previous_position::PreviousPosition,
};

pub mod path;
pub mod timer;

pub struct StepPlugin;

impl Plugin for StepPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(FixedUpdate, advance_step_paths)
            .add_observer(on_step_intent);
    }
}

#[derive(EntityEvent)]
/// Intent requesting a one-tile movement for the target entity.
pub struct StepIntent {
    /// Direction to apply to the entity's current position.
    pub to: Direction,
    #[event_target]
    /// Entity that should receive the step.
    pub entity: Entity,
}

#[derive(EntityEvent)]
/// Event emitted after a step successfully updates the entity position.
pub struct Step(Entity);

#[derive(EntityEvent)]
/// Event emitted when a step crosses from one chunk entity to another.
pub struct StepAcrossChunk {
    /// Chunk that previously contained the entity.
    pub from: Entity,
    /// Chunk that now contains the entity after stepping.
    pub to: Entity,
    #[event_target]
    entity: Entity,
}

fn advance_step_paths(
    mut commands: Commands,
    query: Query<(Entity, &mut StepPath, &mut StepTimer)>,
    time: Res<Time<Fixed>>,
) {
    for (entity, mut path, mut timer) in query {
        // Only consume the next queued step when the per-entity timer finishes.
        timer.tick(time.delta());
        if !timer.is_finished() {
            continue;
        }

        let Some(target_direction) = path.pop() else {
            continue;
        };

        timer.set_duration(Duration::from_secs(1));
        timer.reset();

        commands.trigger(StepIntent {
            to: target_direction,
            entity,
        });
    }
}

fn on_step_intent(
    event: On<StepIntent>,
    mut commands: Commands,
    positions: Query<(&Floor, &Position)>,
    occupancies: Query<&Occupancy>,
    chunks: Res<Chunks>,
) {
    let entity = event.event_target();

    let Ok((layer, position)) = positions.get(entity) else {
        return;
    };

    let target_position = *position + event.to;
    if position == &target_position {
        return;
    }

    let Some(chunk) = chunks.get(position) else {
        return;
    };

    let Some(target_chunk) = chunks.get(&target_position) else {
        return;
    };

    let Ok(occupancy) = occupancies.get(target_chunk) else {
        return;
    };

    if occupancy.contains(layer, &target_position) {
        return;
    }

    commands
        .entity(entity)
        .insert((
            PreviousPosition {
                x: position.x,
                y: position.y,
            },
            Position {
                x: target_position.x,
                y: target_position.y,
            },
        ))
        .trigger(Step);

    if chunk != target_chunk {
        commands.entity(entity).trigger(|entity| StepAcrossChunk {
            from: chunk,
            to: target_chunk,
            entity,
        });
    }
}

#[cfg(test)]
mod tests {
    use suon_chunk::{
        CHUNK_SIZE, Chunk, ChunkPlugin, content::AtChunk, occupancy::occupied::Occupied,
    };

    use super::*;
    use std::time::Duration;

    #[test]
    fn should_advance_path_and_reset_timer_when_finished() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.insert_resource(Time::<Fixed>::default());
        app.add_systems(FixedUpdate, advance_step_paths);

        let mut path = StepPath::default();
        path.push(Direction::North);

        let entity = app
            .world_mut()
            .spawn((path, StepTimer(Timer::from_seconds(0.5, TimerMode::Once))))
            .id();

        // Progress the fixed clock and execute the system schedule to trigger the tick logic
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .advance_by(Duration::from_secs_f32(0.6));

        app.world_mut().run_schedule(FixedUpdate);

        let timer = app
            .world()
            .get::<StepTimer>(entity)
            .expect("StepTimer missing");

        assert_eq!(
            timer.duration(),
            Duration::from_secs(1),
            "Timer duration should be 1s"
        );

        assert!(
            !timer.is_finished(),
            "Timer must be in an active state after reset"
        );

        let path = app
            .world()
            .get::<StepPath>(entity)
            .expect("StepPath missing");

        // The system must pop the direction to trigger the event and reset the timer for the next step
        assert!(
            path.is_empty(),
            "The direction should have been consumed from the path"
        );
    }

    #[test]
    fn should_update_position_and_trigger_step_event_on_successful_intent() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.add_observer(on_step_intent);

        const START_POSITION: Position = Position {
            x: 0,
            y: CHUNK_SIZE as u16 - 1,
        };
        const FLOOR: Floor = Floor { z: 0 };
        let expected_target = START_POSITION + Direction::North;

        let start_chunk = app.world_mut().spawn(Chunk).id();
        let target_chunk = app.world_mut().spawn(Chunk).id();

        app.insert_resource(Chunks::from_iter([
            (START_POSITION, start_chunk),
            (expected_target, target_chunk),
        ]));

        let entity = app.world_mut().spawn((START_POSITION, FLOOR)).id();

        // Trigger the intent event directly to verify the observer's state transition logic
        app.world_mut().trigger(StepIntent {
            entity,
            to: Direction::North,
        });

        app.update();
        app.update();

        let current_position = app
            .world()
            .get::<Position>(entity)
            .expect("Current position missing");

        assert_eq!(
            *current_position, expected_target,
            "Actor should be at the target coordinate"
        );

        let previous_position = app
            .world()
            .get::<PreviousPosition>(entity)
            .expect("Previous position missing");

        assert_eq!(
            previous_position.x, START_POSITION.x,
            "The system must record the starting position as the previous one"
        );

        let at_chunk = app
            .world()
            .get::<AtChunk>(entity)
            .expect("AtChunk reference missing");

        assert_eq!(
            at_chunk.entity(),
            target_chunk,
            "The entity must point to the new chunk entity"
        );
    }

    #[test]
    fn should_prevent_movement_when_target_position_is_occupied() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.add_observer(on_step_intent);

        const MOVE_DIRECTION: Direction = Direction::East;
        const START_POSITION: Position = Position { x: 5, y: 5 };
        const FLOOR: Floor = Floor { z: 0 };
        let target_position = START_POSITION + MOVE_DIRECTION;

        // Map both coordinates to the same chunk for this collision scenario
        let chunk = app.world_mut().spawn(Chunk).id();
        app.insert_resource(Chunks::from_iter([
            (START_POSITION, chunk),
            (target_position, chunk),
        ]));

        // Spawn an obstructing entity marked as Occupied at the target coordinate
        app.world_mut().spawn((target_position, FLOOR, Occupied));

        let entity = app.world_mut().spawn((START_POSITION, FLOOR)).id();

        // Trigger the intent towards the occupied cell
        app.world_mut().trigger(StepIntent {
            entity,
            to: MOVE_DIRECTION,
        });

        app.update();

        let current_position = app.world().get::<Position>(entity).unwrap();

        // The position must remain identical to the starting position
        assert_eq!(
            *current_position, START_POSITION,
            "Movement must be blocked when a target coordinate contains an Occupied entity"
        );
    }

    #[test]
    fn should_fail_when_moving_to_coordinate_without_registered_chunk() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.add_observer(on_step_intent);

        const MOVE_DIRECTION: Direction = Direction::North;
        const START_POSITION: Position = Position { x: 0, y: 0 };
        const FLOOR: Floor = Floor { z: 0 };

        // Only provide a chunk for the starting position
        let chunk = app.world_mut().spawn(Chunk).id();
        app.insert_resource(Chunks::from_iter([(START_POSITION, chunk)]));

        let entity = app.world_mut().spawn((START_POSITION, FLOOR)).id();

        // The observer should panic because the target coordinate is not mapped in ChunkLayer
        app.world_mut().trigger(StepIntent {
            entity,
            to: MOVE_DIRECTION,
        });

        app.update();
    }

    #[test]
    fn should_not_consume_path_or_reset_timer_before_timer_finishes() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.insert_resource(Time::<Fixed>::default());
        app.add_systems(FixedUpdate, advance_step_paths);

        let mut path = StepPath::default();
        path.push(Direction::East);

        let entity = app
            .world_mut()
            .spawn((path, StepTimer(Timer::from_seconds(1.0, TimerMode::Once))))
            .id();

        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .advance_by(Duration::from_secs_f32(0.25));

        app.world_mut().run_schedule(FixedUpdate);

        let timer = app
            .world()
            .get::<StepTimer>(entity)
            .expect("StepTimer missing");
        let path = app
            .world()
            .get::<StepPath>(entity)
            .expect("StepPath missing");

        assert_eq!(
            path.len(),
            1,
            "The queued step should remain available until the timer finishes"
        );

        assert!(
            !timer.is_finished(),
            "The timer should stay in progress when less than its full duration elapses"
        );
    }

    #[test]
    fn should_ignore_step_intent_when_direction_does_not_change_position() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.add_observer(on_step_intent);

        const START_POSITION: Position = Position { x: 0, y: 0 };
        const FLOOR: Floor = Floor { z: 0 };
        let chunk = app.world_mut().spawn(Chunk).id();
        app.insert_resource(Chunks::from_iter([(START_POSITION, chunk)]));

        let entity = app.world_mut().spawn((START_POSITION, FLOOR)).id();

        app.world_mut().trigger(StepIntent {
            entity,
            to: Direction::SouthWest,
        });

        app.update();

        assert!(
            app.world().get::<PreviousPosition>(entity).is_none(),
            "A saturating move that keeps the same coordinate should not record PreviousPosition"
        );

        assert_eq!(
            *app.world()
                .get::<Position>(entity)
                .expect("Position missing"),
            START_POSITION,
            "A no-op step direction should leave the position unchanged"
        );
    }

    #[test]
    fn should_ignore_step_intent_when_entity_has_no_position_or_floor() {
        let mut app = App::new();

        app.add_plugins(ChunkPlugin);
        app.add_observer(on_step_intent);

        let entity = app.world_mut().spawn_empty().id();

        app.world_mut().trigger(StepIntent {
            entity,
            to: Direction::East,
        });

        app.update();

        assert!(
            app.world().get::<Position>(entity).is_none(),
            "Entities without movement components should be ignored by step intents"
        );
    }
}