-
Notifications
You must be signed in to change notification settings - Fork 32
[WIP] A basic day-night cycle that handles music swapping #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,5 @@ | |
|
|
||
| # IntelliJ folder | ||
| .idea | ||
|
|
||
| *.iml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| use amethyst::{ | ||
| core::{Time}, | ||
| ecs::*, | ||
| audio::{SourceHandle}, | ||
| }; | ||
|
|
||
| use crate::Music; | ||
|
|
||
| pub struct DayNightCycle{ | ||
| pub cycle_time: f32, | ||
| pub elapsed_cycle_time: f32, | ||
| pub current_cycle: CycleState, | ||
| pub cycle_data: CycleData, | ||
| } | ||
|
|
||
| impl DayNightCycle { | ||
| pub fn new() -> Self { | ||
| DayNightCycle{ | ||
| cycle_time: 6.0, // FIXME 1 second for testing | ||
| current_cycle : CycleState::Day, | ||
| elapsed_cycle_time: 0.0, | ||
| cycle_data: CycleData { | ||
| day_music_name: "day".into(), | ||
| night_music_name: "night".into(), | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Copy,Clone,Debug)] | ||
| pub enum CycleState{ | ||
| Day, | ||
| Night, | ||
| } | ||
|
|
||
| pub struct CycleData{ | ||
| pub day_music_name: String, | ||
| pub night_music_name: String, | ||
| } | ||
|
|
||
| impl<'s> System<'s> for DayNightCycle{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to split this up into two systems. One that determines if it is day or night and one that switches the music based on that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, thanks! I'll look into events :) |
||
| type SystemData = ( Read<'s, Time>, Write<'s, Music> ); | ||
|
|
||
|
|
||
| fn run(&mut self, (time, mut music): Self::SystemData) { | ||
| self.elapsed_cycle_time += time.delta_real_seconds(); | ||
| if self.elapsed_cycle_time >= self.cycle_time { | ||
| dbg!(&self.current_cycle); | ||
| match self.current_cycle { | ||
| CycleState::Day => { | ||
| self.current_cycle = CycleState::Night; | ||
| music.set_current(Some(self.cycle_data.night_music_name.clone())); | ||
|
|
||
| }, | ||
| CycleState::Night => { | ||
| self.current_cycle = CycleState::Day; | ||
| music.set_current(Some(self.cycle_data.day_music_name.clone())); | ||
| }, | ||
| } | ||
| self.elapsed_cycle_time = 0.0; | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If data is stored in a system, it can only be read or changed by the system or by re-registering the system in the dispatcher. If we add a UI displaying the elapsed cycle time, or if we want to add debug utilities to change the cycle time, we have to change the implementation.
So imo we should not store data in systems, to begin with. Two possible solutions are to store the data in one or multiple components and attach them to an entity. The system would only store the entity so that it can access the components. The second solution would be to store the data as a
Resource.Personally, I dislike storing data in resources because they are less flexible than components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is more flexible about entities/component compared to resources?
The way I understood it is that Resources are for "singletons" while entities are for things that have multiple instances.