mirror of
https://github.com/lisk77/comet.git
synced 2025-12-12 17:18:50 +00:00
refactor(examples): adapt to new entity system
This commit is contained in:
parent
053f1f48da
commit
4e2c18b0c1
3 changed files with 28 additions and 11 deletions
|
|
@ -1,5 +1,12 @@
|
|||
use comet::prelude::*;
|
||||
|
||||
// A general state struct that caches known/static EntityIds
|
||||
// In this example we just cache the EntityId for the text entity
|
||||
#[derive(Default)]
|
||||
struct TextState {
|
||||
text: EntityId,
|
||||
}
|
||||
|
||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||
renderer.init_atlas();
|
||||
// Loading the font from the res/fonts directory with a rendered size of 77px
|
||||
|
|
@ -24,6 +31,11 @@ fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
|||
sRgba::<f32>::from_hex("#abb2bfff"), // Color of the text
|
||||
),
|
||||
);
|
||||
|
||||
// Cache text entity in game state
|
||||
if let Some(state) = app.game_state_mut::<TextState>() {
|
||||
state.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
|
|
@ -32,9 +44,12 @@ fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
|||
let size = renderer.size();
|
||||
|
||||
// Recalculating the position of the text every frame to ensure the same relative position
|
||||
let transform = app.get_component_mut::<Transform2D>(1).unwrap();
|
||||
transform.position_mut().set_x(-((size.width - 50) as f32));
|
||||
transform.position_mut().set_y((size.height - 100) as f32);
|
||||
if let Some(state) = app.game_state::<TextState>() {
|
||||
if let Some(transform) = app.get_component_mut::<Transform2D>(state.text) {
|
||||
transform.position_mut().set_x(-((size.width - 50) as f32));
|
||||
transform.position_mut().set_y((size.height - 100) as f32);
|
||||
}
|
||||
}
|
||||
|
||||
renderer.render_scene_2d(app.scene_mut());
|
||||
}
|
||||
|
|
@ -42,6 +57,7 @@ fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
|||
fn main() {
|
||||
App::new()
|
||||
.with_preset(App2D)
|
||||
.with_game_state(TextState::default())
|
||||
.with_title("Simple Text")
|
||||
.run::<Renderer2D>(setup, update);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue