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