@@ -16,7 +16,7 @@ use wrym::{
1616
1717use dyrah_shared:: {
1818 NetId , TILE_SIZE ,
19- components:: Player ,
19+ components:: { Creature , Player } ,
2020 messages:: { ClientInput , ClientMessage , ServerMessage } ,
2121} ;
2222
@@ -52,6 +52,7 @@ pub struct Game {
5252 // when starting from a stop, wait this long before sending to allow diagonal input
5353 move_start_grace : f32 ,
5454 was_moving : bool ,
55+ creature_tex : Option < usize > ,
5556}
5657
5758impl Game {
@@ -75,12 +76,14 @@ impl Game {
7576 dir_age : [ f32:: MAX ; 4 ] ,
7677 move_start_grace : 0.0 ,
7778 was_moving : false ,
79+ creature_tex : None ,
7880 }
7981 }
8082
8183 pub fn load ( & mut self , gfx : & mut Graphics ) {
8284 self . map . load ( gfx) ;
8385 self . player_tex = Some ( gfx. load_texture ( include_bytes ! ( "../../assets/player.png" ) ) ) ;
86+ self . creature_tex = Some ( gfx. load_texture ( include_bytes ! ( "../../assets/kitty.png" ) ) )
8487 }
8588
8689 pub fn handle_events ( & mut self ) {
@@ -165,6 +168,32 @@ impl Game {
165168 . unwrap_or_else ( || sender_id. to_string ( ) ) ;
166169 self . chat_messages . push ( ( username, text, 0.0 ) ) ;
167170 }
171+ ServerMessage :: CreatureBatchSpawned ( spawns) => {
172+ for spawn in spawns {
173+ self . world . spawn ( (
174+ Creature ,
175+ WorldPos {
176+ vec : spawn. position ,
177+ } ,
178+ TargetWorldPos {
179+ vec : spawn. position ,
180+ path : None ,
181+ } ,
182+ Sprite {
183+ anim : Animation :: new_directional ( 4 , 4 , 0.15 ) ,
184+ frame_size : Vec2 :: splat ( TILE_SIZE ) ,
185+ sprite_size : Vec2 :: splat ( TILE_SIZE ) ,
186+ } ,
187+ ) ) ;
188+ }
189+ }
190+ ServerMessage :: CreatureBatchMoved ( moves) => {
191+ for m in moves {
192+ if let Some ( mut tgt_pos) = self . world . get_mut :: < TargetWorldPos > ( m. id . into ( ) ) {
193+ tgt_pos. vec = m. position ;
194+ }
195+ }
196+ }
168197 }
169198 }
170199
@@ -222,7 +251,6 @@ impl Game {
222251 . mouse_released ( MouseButton :: Left )
223252 . then_some ( mouse_world_pos)
224253 . map ( |mp| self . map . tiled . world_to_tile ( mp. into ( ) ) ) ;
225- let moving = left || up || right || down || mouse_tile_pos. is_some ( ) ;
226254 self . hovered_tile = Some ( self . map . tiled . world_to_tile ( mouse_world_pos) ) ;
227255
228256 self . chat_messages . retain_mut ( |( _, _, age) | {
@@ -231,7 +259,7 @@ impl Game {
231259 } ) ;
232260
233261 // 3 tiles/sec × 32 px/tile = 96 px/sec
234- let move_speed = 3.0 * dyrah_shared :: TILE_SIZE ;
262+ let move_speed = 3.0 * TILE_SIZE ;
235263 let dt = timer. delta ;
236264
237265 self . world . query (
@@ -321,6 +349,44 @@ impl Game {
321349 . send ( & serialize ( & msg) . unwrap ( ) , Reliability :: Unreliable ) ;
322350 }
323351 }
352+
353+ self . world . query (
354+ |_,
355+ _: & Creature ,
356+ pos : & mut WorldPos ,
357+ target_pos : & mut TargetWorldPos ,
358+ spr : & mut Sprite | {
359+ if pos. vec != target_pos. vec {
360+ let diff = target_pos. vec - pos. vec ;
361+ let dist = diff. length ( ) ;
362+ let step = move_speed * dt;
363+ if step >= dist {
364+ pos. vec = target_pos. vec ;
365+ } else {
366+ pos. vec += diff * ( step / dist) ;
367+ }
368+
369+ let dir = if diff. x . abs ( ) > diff. y . abs ( ) {
370+ if diff. x > 0.0 {
371+ Direction :: Right
372+ } else {
373+ Direction :: Left
374+ }
375+ } else {
376+ if diff. y > 0.0 {
377+ Direction :: Down
378+ } else {
379+ Direction :: Up
380+ }
381+ } ;
382+ spr. anim . set_direction ( dir) ;
383+ spr. anim . update_rows ( dt, 2 ) ;
384+ } else {
385+ let col = spr. anim . current % spr. anim . cols ;
386+ spr. anim . current = col;
387+ }
388+ } ,
389+ ) ;
324390 }
325391
326392 pub fn render ( & mut self , gfx : & mut Graphics , egui_ctx : & mut & Context , timer : & FrameTimer ) {
@@ -393,6 +459,17 @@ impl Game {
393459
394460 self . map . draw_tiles ( gfx, player_tile) ;
395461
462+ self . world . query (
463+ |_, _: & dyrah_shared:: components:: Creature , world_pos : & WorldPos , spr : & Sprite | {
464+ let draw_pos = world_pos. vec - TILE_SIZE / 4.0 ;
465+ gfx. rect ( )
466+ . at ( draw_pos)
467+ . size ( Vec2 :: splat ( TILE_SIZE ) )
468+ . texture ( self . creature_tex . unwrap ( ) )
469+ . uv ( spr. anim . frame ( ) ) ;
470+ } ,
471+ ) ;
472+
396473 if let Some ( player) = self . player {
397474 if let Some ( target) = self . world . get :: < TargetWorldPos > ( player) {
398475 if let Some ( path) = & target. path {
0 commit comments