player rendering
This commit is contained in:
parent
51fc8adac1
commit
534c9bd800
3
assets/worlds/mainworld.json
Normal file
3
assets/worlds/mainworld.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -6,7 +6,7 @@ use raylib::{
|
|||||||
camera::Camera2D, math::Vector2, prelude::RaylibDrawHandle, RaylibHandle, RaylibThread,
|
camera::Camera2D, math::Vector2, prelude::RaylibDrawHandle, RaylibHandle, RaylibThread,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::resources::GlobalResources;
|
use crate::{player::Player, resources::GlobalResources, world::World};
|
||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
@ -16,7 +16,8 @@ pub enum GameState {
|
|||||||
Loading,
|
Loading,
|
||||||
MainMenu,
|
MainMenu,
|
||||||
PauseMenu,
|
PauseMenu,
|
||||||
GameQuit
|
GameQuit,
|
||||||
|
InGame
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for GameState {
|
impl fmt::Display for GameState {
|
||||||
@ -40,11 +41,17 @@ pub struct GameCore {
|
|||||||
pub master_camera: Camera2D,
|
pub master_camera: Camera2D,
|
||||||
|
|
||||||
/// Debug features
|
/// Debug features
|
||||||
pub show_simple_debug_info: bool
|
pub show_simple_debug_info: bool,
|
||||||
|
|
||||||
|
/// The world
|
||||||
|
pub world: World,
|
||||||
|
|
||||||
|
/// The player
|
||||||
|
pub player: Player,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameCore {
|
impl GameCore {
|
||||||
pub fn new(raylib: &mut RaylibHandle, thread: &RaylibThread) -> Self {
|
pub fn new(raylib: &mut RaylibHandle, thread: &RaylibThread, world: World) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: GameState::Loading,
|
state: GameState::Loading,
|
||||||
last_state: GameState::Loading,
|
last_state: GameState::Loading,
|
||||||
@ -58,7 +65,9 @@ impl GameCore {
|
|||||||
rotation: 0.0,
|
rotation: 0.0,
|
||||||
zoom: 1.0,
|
zoom: 1.0,
|
||||||
},
|
},
|
||||||
show_simple_debug_info: false
|
show_simple_debug_info: false,
|
||||||
|
world: world,
|
||||||
|
player: Player::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,11 +7,60 @@ use crate::{
|
|||||||
|
|
||||||
use super::screen::Screen;
|
use super::screen::Screen;
|
||||||
|
|
||||||
pub struct InGameScreen {}
|
pub enum InGameState {
|
||||||
|
BUYING,
|
||||||
|
SWIMMING,
|
||||||
|
DEAD,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct InGameScreen {
|
||||||
|
current_state: InGameState,
|
||||||
|
}
|
||||||
|
|
||||||
impl InGameScreen {
|
impl InGameScreen {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {}
|
Self {
|
||||||
|
current_state: InGameState::SWIMMING,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_player_movement(
|
||||||
|
&mut self,
|
||||||
|
draw_handle: &mut RaylibDrawHandle,
|
||||||
|
game_core: &mut GameCore,
|
||||||
|
) {
|
||||||
|
let player_screen_position =
|
||||||
|
draw_handle.get_screen_to_world2D(game_core.player.position, game_core.master_camera);
|
||||||
|
|
||||||
|
// Handle player movement
|
||||||
|
let mouse_pose = draw_handle.get_mouse_position();
|
||||||
|
let mut raw_movement_direction = mouse_pose - player_screen_position;
|
||||||
|
raw_movement_direction.normalize();
|
||||||
|
game_core.player.direction = raw_movement_direction;
|
||||||
|
|
||||||
|
// Handle action buttons
|
||||||
|
let user_request_boost =
|
||||||
|
draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON);
|
||||||
|
let user_request_action =
|
||||||
|
draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_player(
|
||||||
|
&mut self,
|
||||||
|
context_2d: &mut RaylibMode2D<RaylibDrawHandle>,
|
||||||
|
game_core: &mut GameCore,
|
||||||
|
) {
|
||||||
|
// Get the player
|
||||||
|
let player = &game_core.player;
|
||||||
|
|
||||||
|
// TODO: tmp rect
|
||||||
|
context_2d.draw_rectangle(
|
||||||
|
player.position.x as i32 - 10,
|
||||||
|
player.position.y as i32 - 10,
|
||||||
|
20,
|
||||||
|
30,
|
||||||
|
Color::BLACK,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,6 +75,30 @@ impl Screen for InGameScreen {
|
|||||||
// Clear frame
|
// Clear frame
|
||||||
draw_handle.clear_background(Color::WHITE);
|
draw_handle.clear_background(Color::WHITE);
|
||||||
|
|
||||||
|
// Handle the pause menu being opened
|
||||||
|
if draw_handle.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
|
||||||
|
return Some(GameState::PauseMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Window dimensions
|
||||||
|
let win_height = draw_handle.get_screen_height();
|
||||||
|
let win_width = draw_handle.get_screen_width();
|
||||||
|
let window_center = Vector2 {
|
||||||
|
x: (win_width as f32 / 2.0),
|
||||||
|
y: (win_height as f32 / 2.0),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update player movement
|
||||||
|
self.update_player_movement(draw_handle, game_core);
|
||||||
|
|
||||||
|
// Open a 2D context
|
||||||
|
{
|
||||||
|
let mut context_2d = draw_handle.begin_mode2D(game_core.master_camera);
|
||||||
|
|
||||||
|
// Render Player
|
||||||
|
self.render_player(&mut context_2d, game_core);
|
||||||
|
}
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,53 @@ impl Screen for MainMenuScreen {
|
|||||||
audio_system: &mut AudioPlayer,
|
audio_system: &mut AudioPlayer,
|
||||||
game_core: &mut GameCore,
|
game_core: &mut GameCore,
|
||||||
) -> Option<GameState> {
|
) -> Option<GameState> {
|
||||||
|
// Window dimensions
|
||||||
|
let win_height = draw_handle.get_screen_height();
|
||||||
|
let win_width = draw_handle.get_screen_width();
|
||||||
|
|
||||||
// Clear frame
|
// Clear frame
|
||||||
draw_handle.clear_background(Color::WHITE);
|
draw_handle.clear_background(Color::WHITE);
|
||||||
|
|
||||||
// TODO: This is only for testing
|
// Render title
|
||||||
if draw_handle.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
|
draw_handle.draw_text(
|
||||||
return Some(GameState::PauseMenu);
|
"TMP TITLE",
|
||||||
|
(win_height / 2) - 80,
|
||||||
|
win_width / 4,
|
||||||
|
40,
|
||||||
|
Color::BLACK,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Play and quit
|
||||||
|
draw_handle.draw_text(
|
||||||
|
"Play",
|
||||||
|
(win_height / 2) - 80,
|
||||||
|
(win_width / 4) + 100,
|
||||||
|
20,
|
||||||
|
Color::BLACK,
|
||||||
|
);
|
||||||
|
draw_handle.draw_text(
|
||||||
|
"Quit",
|
||||||
|
(win_height / 2) - 80,
|
||||||
|
(win_width / 4) + 140,
|
||||||
|
20,
|
||||||
|
Color::BLACK,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Handle button presses
|
||||||
|
let mouse_position = draw_handle.get_mouse_position();
|
||||||
|
let mouse_clicked = draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON);
|
||||||
|
|
||||||
|
// Check clicks
|
||||||
|
if mouse_clicked {
|
||||||
|
if mouse_position.y > (win_width as f32 / 4.0) + 100.0
|
||||||
|
&& mouse_position.y < (win_width as f32 / 4.0) + 120.0
|
||||||
|
{
|
||||||
|
return Some(GameState::InGame);
|
||||||
|
} else if mouse_position.y > (win_width as f32 / 4.0) + 140.0
|
||||||
|
&& mouse_position.y < (win_width as f32 / 4.0) + 180.0
|
||||||
|
{
|
||||||
|
return Some(GameState::GameQuit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
|
19
src/main.rs
19
src/main.rs
@ -2,15 +2,18 @@ mod gamecore;
|
|||||||
mod lib;
|
mod lib;
|
||||||
mod logic;
|
mod logic;
|
||||||
mod resources;
|
mod resources;
|
||||||
|
mod player;
|
||||||
|
mod world;
|
||||||
|
|
||||||
use gamecore::{GameCore, GameState};
|
use gamecore::{GameCore, GameState};
|
||||||
use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer};
|
use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer};
|
||||||
use log::info;
|
use log::info;
|
||||||
use logic::{
|
use logic::{
|
||||||
loadingscreen::LoadingScreen, mainmenu::MainMenuScreen, pausemenu::PauseMenuScreen,
|
ingame::InGameScreen, loadingscreen::LoadingScreen, mainmenu::MainMenuScreen,
|
||||||
screen::Screen,
|
pausemenu::PauseMenuScreen, screen::Screen,
|
||||||
};
|
};
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
|
use world::World;
|
||||||
|
|
||||||
// Game Launch Configuration
|
// Game Launch Configuration
|
||||||
const DEFAULT_WINDOW_DIMENSIONS: Vector2 = Vector2 {
|
const DEFAULT_WINDOW_DIMENSIONS: Vector2 = Vector2 {
|
||||||
@ -37,8 +40,11 @@ fn main() {
|
|||||||
// Override the default exit key
|
// Override the default exit key
|
||||||
raylib.set_exit_key(None);
|
raylib.set_exit_key(None);
|
||||||
|
|
||||||
|
// Load the world
|
||||||
|
let world = World::load_from_json("./assets/worlds/mainworld.json".to_string()).expect("Failed to load main world JSON");
|
||||||
|
|
||||||
// Set up the game's core state
|
// Set up the game's core state
|
||||||
let mut game_core = GameCore::new(&mut raylib, &raylib_thread);
|
let mut game_core = GameCore::new(&mut raylib, &raylib_thread, world);
|
||||||
|
|
||||||
// Set up the game's profiler
|
// Set up the game's profiler
|
||||||
let mut profiler = GameProfiler::new();
|
let mut profiler = GameProfiler::new();
|
||||||
@ -51,6 +57,7 @@ fn main() {
|
|||||||
let mut loading_screen = LoadingScreen::new();
|
let mut loading_screen = LoadingScreen::new();
|
||||||
let mut main_menu_screen = MainMenuScreen::new();
|
let mut main_menu_screen = MainMenuScreen::new();
|
||||||
let mut pause_menu_screen = PauseMenuScreen::new();
|
let mut pause_menu_screen = PauseMenuScreen::new();
|
||||||
|
let mut ingame_screen = InGameScreen::new();
|
||||||
|
|
||||||
// Main rendering loop
|
// Main rendering loop
|
||||||
while !raylib.window_should_close() {
|
while !raylib.window_should_close() {
|
||||||
@ -77,6 +84,12 @@ fn main() {
|
|||||||
&mut game_core,
|
&mut game_core,
|
||||||
),
|
),
|
||||||
GameState::GameQuit => None,
|
GameState::GameQuit => None,
|
||||||
|
GameState::InGame => ingame_screen.render(
|
||||||
|
&mut draw_handle,
|
||||||
|
&raylib_thread,
|
||||||
|
&mut audio_system,
|
||||||
|
&mut game_core,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
// If needed, update the global state
|
// If needed, update the global state
|
||||||
|
19
src/player.rs
Normal file
19
src/player.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use raylib::math::Vector2;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Player {
|
||||||
|
pub position: Vector2,
|
||||||
|
pub direction: Vector2,
|
||||||
|
pub coins: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Player {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
..Default::default()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
src/world.rs
Normal file
19
src/world.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use std::{fs::File, io::BufReader};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::io::Read;
|
||||||
|
use failure::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct World {}
|
||||||
|
|
||||||
|
impl World {
|
||||||
|
pub fn load_from_json(file: String) -> Result<Self, Error> {
|
||||||
|
// Load the file
|
||||||
|
let file = File::open(file)?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
|
// Deserialize
|
||||||
|
Ok(serde_json::from_reader(reader)?)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user