mirror of
				https://github.com/pcvolkmer/winelounge.git
				synced 2025-10-30 19:06:11 +00:00 
			
		
		
		
	Add sprite handling
This commit is contained in:
		| @@ -1,4 +1,5 @@ | |||||||
| mod player; | mod player; | ||||||
|  | mod sprite; | ||||||
| mod world; | mod world; | ||||||
|  |  | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
| @@ -107,8 +108,7 @@ fn main() { | |||||||
|             } else if content == BoxAreaContent::FilledBottle && world.player.can_fill_glass() { |             } else if content == BoxAreaContent::FilledBottle && world.player.can_fill_glass() { | ||||||
|                 ba.update_content(BoxAreaContent::EmptyBottle); |                 ba.update_content(BoxAreaContent::EmptyBottle); | ||||||
|                 world.player.fill_glass(); |                 world.player.fill_glass(); | ||||||
|             } else if content == BoxAreaContent::FilledBottle && !world.player.can_fill_glass() |             } else if content == BoxAreaContent::FilledBottle && !world.player.can_fill_glass() { | ||||||
|             { |  | ||||||
|                 ba.update_content(BoxAreaContent::FilledBottle); |                 ba.update_content(BoxAreaContent::FilledBottle); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -1,5 +1,7 @@ | |||||||
| use crate::GLASS_SPACE; | use crate::sprite::Sprite; | ||||||
|  | use crate::{sprite, GLASS_SPACE}; | ||||||
| use sdl2::rect::{Point, Rect}; | use sdl2::rect::{Point, Rect}; | ||||||
|  | use sdl2::render::{Texture, WindowCanvas}; | ||||||
|  |  | ||||||
| pub struct Player { | pub struct Player { | ||||||
|     pub position: Point, |     pub position: Point, | ||||||
| @@ -65,19 +67,26 @@ impl Player { | |||||||
|         Rect::new(self.position.x(), self.position.y(), 40, 115) |         Rect::new(self.position.x(), self.position.y(), 40, 115) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn sprite(&self) -> Rect { |     pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture) { | ||||||
|         let x = match self.footstep { |         let direction = match self.direction { | ||||||
|             1 => 60, |             PlayerDirection::Down => sprite::PlayerDirection::Down, | ||||||
|             2 => 115, |             PlayerDirection::Left => sprite::PlayerDirection::Left, | ||||||
|             _ => 5, |             PlayerDirection::Right => sprite::PlayerDirection::Right, | ||||||
|  |             PlayerDirection::Up => sprite::PlayerDirection::Up, | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         match self.direction { |         let footstep = match self.footstep { | ||||||
|             PlayerDirection::Down => Rect::new(x, 5, 40, 115), |             1 => sprite::PlayerFootstep::Left, | ||||||
|             PlayerDirection::Left => Rect::new(x, 255, 40, 115), |             2 => sprite::PlayerFootstep::Right, | ||||||
|             PlayerDirection::Right => Rect::new(x, 130, 40, 115), |             _ => sprite::PlayerFootstep::None, | ||||||
|             PlayerDirection::Up => Rect::new(x, 380, 40, 115), |         }; | ||||||
|         } |  | ||||||
|  |         Sprite::Player(direction, footstep).render( | ||||||
|  |             canvas, | ||||||
|  |             texture, | ||||||
|  |             self.position.x(), | ||||||
|  |             self.position.y(), | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn face_up(&mut self) { |     pub fn face_up(&mut self) { | ||||||
|   | |||||||
							
								
								
									
										73
									
								
								src/sprite.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/sprite.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | |||||||
|  | use sdl2::rect::Rect; | ||||||
|  | use sdl2::render::{Texture, WindowCanvas}; | ||||||
|  |  | ||||||
|  | pub enum Sprite { | ||||||
|  |     BottleEmpty, | ||||||
|  |     BottleFilled, | ||||||
|  |     BushHorizontal, | ||||||
|  |     BushVertical, | ||||||
|  |     Flower, | ||||||
|  |     GlassEmpty, | ||||||
|  |     GlassFilled, | ||||||
|  |     HiddenBox, | ||||||
|  |     Lounge, | ||||||
|  |     Nothing, | ||||||
|  |     Player(PlayerDirection, PlayerFootstep), | ||||||
|  |     Stone, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | pub enum PlayerFootstep { | ||||||
|  |     None = 5, | ||||||
|  |     Left = 60, | ||||||
|  |     Right = 115, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | pub enum PlayerDirection { | ||||||
|  |     Down, | ||||||
|  |     Left, | ||||||
|  |     Right, | ||||||
|  |     Up, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | impl Sprite { | ||||||
|  |     pub fn rect(&self) -> Rect { | ||||||
|  |         match &self { | ||||||
|  |             Sprite::BottleEmpty => Rect::new(35, 550, 20, 50), | ||||||
|  |             Sprite::BottleFilled => Rect::new(5, 550, 20, 50), | ||||||
|  |             Sprite::BushHorizontal => Rect::new(70, 510, 50, 25), | ||||||
|  |             Sprite::BushVertical => Rect::new(70, 550, 25, 50), | ||||||
|  |             Sprite::Flower => Rect::new(130, 550, 25, 25), | ||||||
|  |             Sprite::GlassEmpty => Rect::new(35, 510, 20, 25), | ||||||
|  |             Sprite::GlassFilled => Rect::new(5, 510, 20, 25), | ||||||
|  |             Sprite::HiddenBox => Rect::new(5, 620, 50, 50), | ||||||
|  |             Sprite::Lounge => Rect::new(5, 700, 150, 95), | ||||||
|  |             Sprite::Nothing => Rect::new(70, 620, 50, 50), | ||||||
|  |             Sprite::Stone => Rect::new(130, 510, 25, 25), | ||||||
|  |             Sprite::Player(pd, fs) => Sprite::player_rect(pd, fs), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn size(&self) -> (u32, u32) { | ||||||
|  |         self.rect().size() | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn render(&self, canvas: &mut WindowCanvas, texture: &Texture, x: i32, y: i32) { | ||||||
|  |         let render_rect = Rect::new(x, y, self.rect().width(), self.rect().height()); | ||||||
|  |         let _r = canvas.copy(texture, self.rect(), render_rect); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fn player_rect(player_direction: &PlayerDirection, footstep: &PlayerFootstep) -> Rect { | ||||||
|  |         let x = match footstep { | ||||||
|  |             PlayerFootstep::Left => 60, | ||||||
|  |             PlayerFootstep::Right => 115, | ||||||
|  |             _ => 5, | ||||||
|  |         }; | ||||||
|  |  | ||||||
|  |         match player_direction { | ||||||
|  |             PlayerDirection::Down => Rect::new(x, 5, 40, 115), | ||||||
|  |             PlayerDirection::Left => Rect::new(x, 255, 40, 115), | ||||||
|  |             PlayerDirection::Right => Rect::new(x, 130, 40, 115), | ||||||
|  |             PlayerDirection::Up => Rect::new(x, 380, 40, 115), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										123
									
								
								src/world.rs
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								src/world.rs
									
									
									
									
									
								
							| @@ -1,3 +1,4 @@ | |||||||
|  | use crate::sprite::Sprite; | ||||||
| use crate::{Player, GLASS_SPACE}; | use crate::{Player, GLASS_SPACE}; | ||||||
| use sdl2::pixels::Color; | use sdl2::pixels::Color; | ||||||
| use sdl2::rect::{Point, Rect}; | use sdl2::rect::{Point, Rect}; | ||||||
| @@ -146,27 +147,15 @@ impl World { | |||||||
|             canvas.fill_rect(Rect::new(5, 37, GLASS_SPACE as u32 * 25 + 5, 4)); |             canvas.fill_rect(Rect::new(5, 37, GLASS_SPACE as u32 * 25 + 5, 4)); | ||||||
|  |  | ||||||
|             if self.player.filled_glasses + self.player.empty_glasses >= i { |             if self.player.filled_glasses + self.player.empty_glasses >= i { | ||||||
|                 canvas.copy( |                 Sprite::GlassEmpty.render(canvas, texture, (i as i32) * 25 - 15, 10); | ||||||
|                     texture, |  | ||||||
|                     Rect::new(35, 510, 20, 25), |  | ||||||
|                     Rect::new((i as i32) * 25 - 15, 10, 20, 25), |  | ||||||
|                 ); |  | ||||||
|             } |             } | ||||||
|             if self.player.filled_glasses >= i { |             if self.player.filled_glasses >= i { | ||||||
|                 canvas.copy( |                 Sprite::GlassFilled.render(canvas, texture, (i as i32) * 25 - 15, 10); | ||||||
|                     texture, |  | ||||||
|                     Rect::new(5, 510, 20, 25), |  | ||||||
|                     Rect::new((i as i32) * 25 - 15, 10, 20, 25), |  | ||||||
|                 ); |  | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         // Lounge |         // Lounge | ||||||
|         canvas.copy( |         Sprite::Lounge.render(canvas, texture, 325, 260); | ||||||
|             texture, |  | ||||||
|             Rect::new(5, 700, 150, 95), |  | ||||||
|             Rect::new(325, 260, 150, 95), |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         // Box Areas |         // Box Areas | ||||||
|         self.right_top_box_area.render(canvas, texture); |         self.right_top_box_area.render(canvas, texture); | ||||||
| @@ -175,52 +164,20 @@ impl World { | |||||||
|         self.left_top_box_area.render(canvas, texture); |         self.left_top_box_area.render(canvas, texture); | ||||||
|  |  | ||||||
|         // Decoration |         // Decoration | ||||||
|         canvas.copy( |         Sprite::Flower.render(canvas, texture, 235, 130); | ||||||
|             texture, |         Sprite::Flower.render(canvas, texture, 120, 210); | ||||||
|             Rect::new(130, 550, 25, 25), |         Sprite::Flower.render(canvas, texture, 535, 150); | ||||||
|             Rect::new(235, 130, 25, 25), |         Sprite::Flower.render(canvas, texture, 435, 370); | ||||||
|         ); |         Sprite::Flower.render(canvas, texture, 235, 470); | ||||||
|         canvas.copy( |         Sprite::Flower.render(canvas, texture, 555, 510); | ||||||
|             texture, |  | ||||||
|             Rect::new(130, 550, 25, 25), |  | ||||||
|             Rect::new(120, 210, 25, 25), |  | ||||||
|         ); |  | ||||||
|         canvas.copy( |  | ||||||
|             texture, |  | ||||||
|             Rect::new(130, 550, 25, 25), |  | ||||||
|             Rect::new(535, 150, 25, 25), |  | ||||||
|         ); |  | ||||||
|         canvas.copy( |  | ||||||
|             texture, |  | ||||||
|             Rect::new(130, 550, 25, 25), |  | ||||||
|             Rect::new(435, 370, 25, 25), |  | ||||||
|         ); |  | ||||||
|         canvas.copy( |  | ||||||
|             texture, |  | ||||||
|             Rect::new(130, 550, 25, 25), |  | ||||||
|             Rect::new(235, 470, 25, 25), |  | ||||||
|         ); |  | ||||||
|         canvas.copy( |  | ||||||
|             texture, |  | ||||||
|             Rect::new(130, 550, 25, 25), |  | ||||||
|             Rect::new(555, 510, 25, 25), |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         // Stops |         // Stops | ||||||
|         for s in &self.stops { |         for s in &self.stops { | ||||||
|             canvas.copy( |             Sprite::Stone.render(canvas, texture, s.x(), s.y()) | ||||||
|                 texture, |  | ||||||
|                 Rect::new(130, 510, 25, 25), |  | ||||||
|                 Rect::new(s.x(), s.y(), 25, 25), |  | ||||||
|             ); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Player |         // Player | ||||||
|         canvas.copy( |         self.player.render(canvas, texture); | ||||||
|             texture, |  | ||||||
|             self.player.sprite(), |  | ||||||
|             Rect::new(self.player.position.x(), self.player.position.y(), 40, 115), |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         // Points |         // Points | ||||||
|         let x = font |         let x = font | ||||||
| @@ -311,48 +268,30 @@ impl BoxArea { | |||||||
|         let y_offset = self.bounding_rect().y(); |         let y_offset = self.bounding_rect().y(); | ||||||
|  |  | ||||||
|         // Border |         // Border | ||||||
|         canvas.copy( |         Sprite::BushHorizontal.render(canvas, texture, x_offset + 30, y_offset); | ||||||
|             texture, |         Sprite::BushHorizontal.render(canvas, texture, x_offset + 30, y_offset + 85); | ||||||
|             Rect::new(70, 510, 50, 25), |         let (dst_x, dst_y) = match self.position { | ||||||
|             Rect::new(x_offset + 30, y_offset, 50, 25), |             BoxAreaPosition::RightTop => (x_offset + 85, y_offset + 30), | ||||||
|         ); |             BoxAreaPosition::RightBottom => (x_offset + 85, y_offset + 30), | ||||||
|         canvas.copy( |             BoxAreaPosition::LeftBottom => (x_offset, y_offset + 30), | ||||||
|             texture, |             BoxAreaPosition::LeftTop => (x_offset, y_offset + 30), | ||||||
|             Rect::new(70, 510, 50, 25), |  | ||||||
|             Rect::new(x_offset + 30, y_offset + 85, 50, 25), |  | ||||||
|         ); |  | ||||||
|         let dst = match self.position { |  | ||||||
|             BoxAreaPosition::RightTop => Rect::new(x_offset + 85, y_offset + 30, 25, 50), |  | ||||||
|             BoxAreaPosition::RightBottom => Rect::new(x_offset + 85, y_offset + 30, 25, 50), |  | ||||||
|             BoxAreaPosition::LeftBottom => Rect::new(x_offset, y_offset + 30, 25, 50), |  | ||||||
|             BoxAreaPosition::LeftTop => Rect::new(x_offset, y_offset + 30, 25, 50), |  | ||||||
|         }; |         }; | ||||||
|         canvas.copy(texture, Rect::new(70, 550, 25, 50), dst); |         Sprite::BushVertical.render(canvas, texture, dst_x, dst_y); | ||||||
|  |  | ||||||
|         // Box |         // Box | ||||||
|         let box_src = match self.content { |         let box_sprite = match &self.content { | ||||||
|             BoxAreaContent::Nothing => Rect::new(70, 620, 50, 50), |             BoxAreaContent::HiddenBox => Sprite::HiddenBox, | ||||||
|             BoxAreaContent::HiddenBox => Rect::new(5, 620, 50, 50), |             BoxAreaContent::FilledBottle => Sprite::BottleFilled, | ||||||
|             BoxAreaContent::EmptyGlass => Rect::new(35, 510, 20, 25), |             BoxAreaContent::EmptyBottle => Sprite::BottleEmpty, | ||||||
|             BoxAreaContent::FilledBottle => Rect::new(5, 550, 20, 50), |             BoxAreaContent::EmptyGlass => Sprite::GlassEmpty, | ||||||
|             BoxAreaContent::EmptyBottle => Rect::new(35, 550, 20, 50), |             BoxAreaContent::Nothing => Sprite::Nothing, | ||||||
|         }; |         }; | ||||||
|         let (box_width, box_height) = match self.content { |         let (box_width, box_height) = box_sprite.size(); | ||||||
|             BoxAreaContent::Nothing => (50, 50), |         box_sprite.render( | ||||||
|             BoxAreaContent::HiddenBox => (50, 50), |             canvas, | ||||||
|             BoxAreaContent::EmptyGlass => (20, 25), |  | ||||||
|             BoxAreaContent::FilledBottle => (20, 50), |  | ||||||
|             BoxAreaContent::EmptyBottle => (20, 50), |  | ||||||
|         }; |  | ||||||
|         canvas.copy( |  | ||||||
|             texture, |             texture, | ||||||
|             box_src, |             x_offset + 30 + (50 - box_width as i32) / 2, | ||||||
|             Rect::new( |             y_offset + 30 + (50 - box_height as i32) / 2, | ||||||
|                 x_offset + 30 + (50 - box_width) / 2, |  | ||||||
|                 y_offset + 30 + (50 - box_height) / 2, |  | ||||||
|                 box_width as u32, |  | ||||||
|                 box_height as u32, |  | ||||||
|             ), |  | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user