Skip to content

Commit 1cde48f

Browse files
committed
fix crash due to resources
1 parent 13171a9 commit 1cde48f

File tree

7 files changed

+20
-15
lines changed

7 files changed

+20
-15
lines changed

src/gamestate/gameover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ pub fn process(gs: &mut GameState) {
2222
DARKGRAY,
2323
);
2424
if is_key_pressed(KeyCode::Enter) {
25-
*gs = GameState::new();
25+
*gs = GameState::new(gs.assets.clone());
2626
}
2727
}

src/gamestate/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::entity::{EntityId, EntityStats, SpawnCommand};
1313
use crate::player::Player;
1414
use crate::projectile::{Projectile, ProjectileStats, ProjectileType};
1515
use crate::roto_script::{GameConstants, RotoScriptManager};
16-
use crate::visual_config::GameVisualConfig;
16+
use crate::visual_config::{Assets, GameVisualConfig};
1717

1818
#[derive(Clone, Copy, Debug, PartialEq)]
1919
pub enum GameStateEnum {
@@ -46,10 +46,11 @@ pub struct GameState {
4646
pub enemies_to_despawn: HashSet<EntityId>,
4747
pub projectiles_to_despawn: HashSet<EntityId>,
4848
pub message_from_elf: Option<String>,
49+
pub assets: Assets,
4950
}
5051

5152
impl GameState {
52-
pub fn new() -> Self {
53+
pub fn new(assets: Assets) -> Self {
5354
let mut roto_manager = RotoScriptManager::new();
5455

5556
// Try to fetch player stats from Roto, fallback to defaults if it fails
@@ -123,6 +124,7 @@ I will summon magic to to beat the evil!.
123124
enemies_to_despawn: HashSet::new(),
124125
projectiles_to_despawn: HashSet::new(),
125126
message_from_elf: Some(tmp.to_owned()),
127+
assets,
126128
}
127129
}
128130

@@ -589,7 +591,7 @@ Just save the file and Press Return here!
589591

590592
pub fn draw_elf_message(gs: &GameState) -> bool {
591593
if let Some(msg) = &gs.message_from_elf {
592-
let texture = &gs.visual_config.char_tex.as_ref().unwrap();
594+
let texture = &gs.assets.char_tex.as_ref().unwrap();
593595

594596
let mut params = DrawTextureParams::default();
595597
let (w, h, s) = (texture.width(), texture.height(), 0.33);

src/gamestate/script_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ pub fn process(gs: &mut GameState) {
3838
DARKGRAY,
3939
);
4040
if is_key_pressed(KeyCode::Enter) {
41-
*gs = GameState::new();
41+
*gs = GameState::new(gs.assets.clone());
4242
}
4343
}

src/gamestate/won.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn process(gs: &mut GameState) {
8080

8181
// Handle restart
8282
if is_key_pressed(KeyCode::Enter) {
83-
*gs = GameState::new();
83+
let assets = gs.assets.clone();
84+
*gs = GameState::new(assets);
8485
}
8586
}

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ mod weapon;
1414

1515
use gamestate::{GameState, GameStateEnum};
1616

17+
use crate::visual_config::Assets;
18+
1719
pub const DT: f64 = 1.0 / 30.0;
1820

1921
fn window_conf() -> Conf {
@@ -40,9 +42,9 @@ async fn main() {
4042
}
4143
}
4244

43-
let mut gs = GameState::new();
44-
45-
gs.visual_config.char_tex = Some(Rc::new(load_texture("assets/elf_char.png").await.unwrap()));
45+
let mut gs = GameState::new(Assets {
46+
char_tex: Some(load_texture("assets/elf_char.png").await.unwrap()),
47+
});
4648

4749
loop {
4850
match gs.state {

src/roto_script.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ impl RotoScriptManager {
111111
pulse: pulse.0,
112112
homing_missile: homing_missile.0,
113113
pulse_blend: pulse_blend.0,
114-
char_tex: None,
115114
})
116115
}
117116
}

src/visual_config.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::rc::Rc;
2-
31
use macroquad::prelude::*;
42

53
use crate::projectile::ProjectileType;
@@ -165,7 +163,7 @@ impl BlendConfig {
165163
}
166164

167165
/// Complete visual configuration for the game
168-
#[derive(Debug, Clone)]
166+
#[derive(Debug, Clone, Copy)]
169167
pub struct GameVisualConfig {
170168
pub player: PlayerVisualConfig,
171169
pub basic_enemy: EnemyVisualConfig,
@@ -174,7 +172,11 @@ pub struct GameVisualConfig {
174172
pub pulse: ProjectileVisualConfig,
175173
pub homing_missile: ProjectileVisualConfig,
176174
pub pulse_blend: BlendConfig,
177-
pub char_tex: Option<Rc<Texture2D>>,
175+
}
176+
177+
#[derive(Debug, Clone, Default)]
178+
pub struct Assets {
179+
pub char_tex: Option<Texture2D>,
178180
}
179181

180182
impl GameVisualConfig {
@@ -187,7 +189,6 @@ impl GameVisualConfig {
187189
pulse: ProjectileVisualConfig::from(ProjectileType::Pulse),
188190
homing_missile: ProjectileVisualConfig::from(ProjectileType::HomingMissile),
189191
pulse_blend: BlendConfig::pulse_default(),
190-
char_tex: None,
191192
}
192193
}
193194
}

0 commit comments

Comments
 (0)