basic bg texturing

This commit is contained in:
Evan Pratten 2021-10-02 09:51:22 -04:00
parent 2b95cbb3b3
commit f53daafa09
4 changed files with 16 additions and 6 deletions

View File

@ -1,7 +1,7 @@
use dirty_fsm::{Action, ActionFlag}; use dirty_fsm::{Action, ActionFlag};
use raylib::prelude::*; use raylib::prelude::*;
use crate::{character::{CharacterState, MainCharacter}, context::GameContext, utilities::render_layer::{FrameUpdate, ScreenSpaceRender, WorldSpaceRender}}; use crate::{character::{CharacterState, MainCharacter}, context::GameContext, utilities::{render_layer::{FrameUpdate, ScreenSpaceRender, WorldSpaceRender}, world_paint_texture::WorldPaintTexture}};
use super::{Scenes, ScreenError}; use super::{Scenes, ScreenError};
use tracing::{debug, trace}; use tracing::{debug, trace};
@ -14,11 +14,12 @@ mod world;
pub struct InGameScreen { pub struct InGameScreen {
camera: Camera2D, camera: Camera2D,
player: MainCharacter, player: MainCharacter,
world_background: WorldPaintTexture
} }
impl InGameScreen { impl InGameScreen {
/// Construct a new `InGameScreen` /// Construct a new `InGameScreen`
pub fn new(player_sprite_sheet: Texture2D) -> Self { pub fn new(player_sprite_sheet: Texture2D, background_texture: Texture2D) -> Self {
Self { Self {
camera: Camera2D { camera: Camera2D {
offset: Vector2::zero(), offset: Vector2::zero(),
@ -27,6 +28,7 @@ impl InGameScreen {
zoom: 1.0, zoom: 1.0,
}, },
player: MainCharacter::new(Vector2::new(0.0, -80.0), player_sprite_sheet), player: MainCharacter::new(Vector2::new(0.0, -80.0), player_sprite_sheet),
world_background: WorldPaintTexture::new(background_texture)
} }
} }
} }

View File

@ -15,8 +15,9 @@ impl WorldSpaceRender for InGameScreen {
config: &GameConfig, config: &GameConfig,
) { ) {
puffin::profile_function!(); puffin::profile_function!();
// Render the player
render_character_in_camera_space(raylib, &self.player, &config); // Render the world background
self.world_background.render(raylib, Vector2::new(0.0, -1080.0), &self.camera);
// Render the floor as a line // Render the floor as a line
let screen_world_zero = raylib.get_screen_to_world2D(Vector2::zero(), self.camera); let screen_world_zero = raylib.get_screen_to_world2D(Vector2::zero(), self.camera);
@ -30,5 +31,9 @@ impl WorldSpaceRender for InGameScreen {
5, 5,
config.colors.white, config.colors.white,
); );
// Render the player
render_character_in_camera_space(raylib, &self.player, &config);
} }
} }

View File

@ -46,6 +46,8 @@ pub fn build_screen_state_machine(
// Load the various textures needed by the states // Load the various textures needed by the states
let player_sprite_sheet = let player_sprite_sheet =
load_texture_from_internal_data(raylib_handle, thread, "character/player_run.png").unwrap(); load_texture_from_internal_data(raylib_handle, thread, "character/player_run.png").unwrap();
let world_background =
load_texture_from_internal_data(raylib_handle, thread, "default-texture.png").unwrap();
// Set up the state machine // Set up the state machine
let mut machine = StateMachine::new(); let mut machine = StateMachine::new();
@ -55,6 +57,6 @@ pub fn build_screen_state_machine(
LoadingScreen::new(raylib_handle, thread)?, LoadingScreen::new(raylib_handle, thread)?,
)?; )?;
machine.add_action(Scenes::MainMenuScreen, MainMenuScreen::new())?; machine.add_action(Scenes::MainMenuScreen, MainMenuScreen::new())?;
machine.add_action(Scenes::InGameScene, InGameScreen::new(player_sprite_sheet))?; machine.add_action(Scenes::InGameScene, InGameScreen::new(player_sprite_sheet, world_background))?;
Ok(machine) Ok(machine)
} }

View File

@ -11,6 +11,7 @@ use raylib::{
use super::non_ref_raylib::HackedRaylibHandle; use super::non_ref_raylib::HackedRaylibHandle;
#[derive(Debug)]
pub struct WorldPaintTexture { pub struct WorldPaintTexture {
texture: Texture2D, texture: Texture2D,
} }
@ -39,7 +40,7 @@ impl WorldPaintTexture {
let left_tile_x = let left_tile_x =
(left_edge_distance / self.texture.width as f32).floor() * self.texture.width as f32; (left_edge_distance / self.texture.width as f32).floor() * self.texture.width as f32;
let right_tile_x = let right_tile_x =
(right_edge_distance / self.texture.width as f32).ceil() * self.texture.width as f32; left_tile_x + self.texture.width as f32;
// Render the tiles // Render the tiles
raylib.draw_texture_v( raylib.draw_texture_v(