Skip to content
This repository was archived by the owner on Aug 14, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/ecs/game_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::{atlas::Atlas, utils::constants::*};
use super::{
atlas::{self},
sprites::player_sprite::PlayerSprite,
sprites::npc_sprite::NpcSprite,
sprites::tile_sprite::{create_tiles, TileSprite},
atlas,
sprites::{
interface_sprite::InterfaceSprite,
npc_sprite::NpcSprite,
player_sprite::PlayerSprite,
tile_sprite::{create_tiles, TileSprite},
},
systems::{
input_system::input_system,
input_system::interaction::*,
Expand Down Expand Up @@ -33,13 +36,15 @@ pub struct GameState {
npcs_sprite: NpcSprite,
npcs_sprite_batch: SpriteBatch,
world_sprite_batch: SpriteBatch,
interface_sprite: InterfaceSprite,
interface_sprite_batch: SpriteBatch,
frames: usize,
//lives state
}

impl ggez::event::EventHandler<GameError> for GameState {
fn update(&mut self, ctx: &mut Context) -> GameResult {
let player_mov_actions = input_system::player_movements(ctx);

match self.current_interaction {
Some(_) => (),
None => {
Expand All @@ -62,6 +67,7 @@ impl ggez::event::EventHandler<GameError> for GameState {
draw_objects(ctx, &self.camera, &self.physics_components)?;
draw_npcs(ctx, &self.camera, &self.physics_components, &self.npcs_components, &mut self.npcs_sprite_batch, &mut self.npcs_sprite, draw_param)?;
draw_interactions(ctx, &self.camera.size, &self.npcs_components, &self.current_interaction, &self.player_physics.current_focus)?;
draw_lives_counter(ctx, &mut self.interface_sprite_batch, &mut self.interface_sprite, draw_param)?;

graphics::present(ctx)?;

Expand All @@ -86,14 +92,17 @@ impl GameState {
Atlas::parse_atlas_json(std::path::Path::new("src/resources/player64.json"));
let player_sprite_batch = atlas::create_batch_sprite(ctx, "/player64.png".to_string());

let npcs_atlas =
Atlas::parse_atlas_json(std::path::Path::new("src/resources/npcs64.json"));
let npcs_atlas = Atlas::parse_atlas_json(std::path::Path::new("src/resources/npcs64.json"));
let npcs_sprite_batch = atlas::create_batch_sprite(ctx, "/npcs64.png".to_string());

let world_atlas =
Atlas::parse_atlas_json(std::path::Path::new("src/resources/world_atlas.json"));
let world_sprite_batch = atlas::create_batch_sprite(ctx, "/world_atlas.png".to_string());

let interface_atlas =
Atlas::parse_atlas_json(std::path::Path::new("src/resources/interface.json"));
let interface_sprite_batch = atlas::create_batch_sprite(ctx, "/interface.png".to_string());

let camera = Camera::new(player_physics.position.clone());

let mut game_state = GameState {
Expand All @@ -112,6 +121,8 @@ impl GameState {
npcs_sprite: NpcSprite::new(&npcs_atlas),
npcs_sprite_batch,
world_sprite_batch,
interface_sprite: InterfaceSprite::new(&interface_atlas),
interface_sprite_batch,
tiles: create_tiles(&world_atlas),
frames: 0,
};
Expand Down Expand Up @@ -170,11 +181,3 @@ impl GameState {
self.npcs_interactions.push(interaction);
}
}

// TO DO
// - Sprites
// - Interactions boxes
// - dialog box, avatar box
// - desks
// - npcs
// - re review logic
38 changes: 38 additions & 0 deletions src/ecs/sprites/interface_sprite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use ggez::{
graphics::spritebatch::SpriteBatch,
mint::{Point2, Vector2},
};
use super::super::atlas;
use super::sprite::Sprite;

pub struct InterfaceSprite {
life_outline_sprite: Sprite,
life_filled_sprite: Sprite

}

impl InterfaceSprite {
pub fn new(atlas: &atlas::Atlas) -> Self {
Self {
life_outline_sprite: atlas.create_sprite(&"heart-0".to_string()),
life_filled_sprite: atlas.create_sprite(&"heart-1".to_string()),
}
}
pub fn draw(
&mut self,
batch: &mut SpriteBatch,
) {
// let s: &mut Sprite = &mut self.sprites[npc.id as usize];
// let position = camera.world_to_screen(&physics.position);

let sprite: &mut Sprite = &mut self.life_filled_sprite;

batch.add(sprite.draw_params(
Point2 {
x: 600.0,
y: 600.0,
},
Vector2 { x: 2.0, y: 2.0 },
));
}
}
16 changes: 15 additions & 1 deletion src/ecs/systems/render_system/render_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::super::super::utils::constants::NPC_COUNT;
use crate::ecs::{
components::npc::Npc,
game_state::EntityIndex,
sprites::{player_sprite::PlayerSprite, tile_sprite::TileSprite, npc_sprite::NpcSprite},
sprites::{player_sprite::PlayerSprite, tile_sprite::TileSprite, npc_sprite::NpcSprite, interface_sprite::InterfaceSprite},
};
use ggez::{*, self, Context, GameResult, graphics::{Color, DrawMode, DrawParam, Rect, StrokeOptions, TextFragment, spritebatch::SpriteBatch}};

Expand Down Expand Up @@ -118,6 +118,20 @@ fn draw_object(ctx: &mut Context, physics: &Physics, camera: &Camera) -> GameRes
Ok(())
}

pub fn draw_lives_counter(
ctx: &mut Context,
interface_sprite_batch: &mut SpriteBatch,
interface_sprite: &mut InterfaceSprite,
draw_param: graphics::DrawParam
) -> GameResult {
interface_sprite.draw(interface_sprite_batch);

graphics::draw(ctx, interface_sprite_batch, draw_param)?;
interface_sprite_batch.clear();

Ok(())
}

pub fn draw_interactions(
ctx: &mut Context,
camera_size: &Size,
Expand Down
2 changes: 2 additions & 0 deletions src/ecs/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ pub const DEFAULT_CAMERA_W: f32 = DEFAULT_WINDOW_W;
pub const DEFAULT_CAMERA_H: f32 = DEFAULT_WINDOW_H;

pub const NPC_COUNT: i32 = 4;

// initial live count
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod ecs {
pub mod player_sprite;
pub mod sprite;
pub mod tile_sprite;
pub mod interface_sprite;
}

pub mod systems {
Expand Down
Binary file added src/resources/interface.aseprite
Binary file not shown.
37 changes: 37 additions & 0 deletions src/resources/interface.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ "frames": [
{
"filename": "heart-0",
"frame": { "x": 0, "y": 0, "w": 16, "h": 16 },
"rotated": false,
"trimmed": false,
"spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 },
"sourceSize": { "w": 16, "h": 16 },
"duration": 100
},
{
"filename": "heart-1",
"frame": { "x": 16, "y": 0, "w": 16, "h": 16 },
"rotated": false,
"trimmed": false,
"spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 },
"sourceSize": { "w": 16, "h": 16 },
"duration": 100
}
],
"meta": {
"app": "https://www.aseprite.org/",
"version": "1.2.32-x64",
"image": "interface.png",
"format": "RGBA8888",
"size": { "w": 32, "h": 16 },
"scale": "1",
"frameTags": [
{ "name": "heart", "from": 0, "to": 1, "direction": "forward" }
],
"layers": [
{ "name": "Layer 1", "opacity": 255, "blendMode": "normal" }
],
"slices": [
]
}
}
Binary file added src/resources/interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.