formatted

This commit is contained in:
Kasper Juul Hermansen 2022-01-27 22:38:44 +01:00
parent a1ffc7b95d
commit e68fa87fc9
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
8 changed files with 94 additions and 31 deletions

View File

@ -134,4 +134,4 @@ pub struct SerializeMe;
#[derive(Component, Serialize, Deserialize, Clone)] #[derive(Component, Serialize, Deserialize, Clone)]
pub struct SerializationHelper { pub struct SerializationHelper {
pub map: super::map::Map, pub map: super::map::Map,
} }

View File

@ -1,14 +1,14 @@
use rltk::{Point, VirtualKeyCode};
use rltk::RGB;
use rltk::Rltk; use rltk::Rltk;
use rltk::RGB;
use rltk::{Point, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
use crate::{CombatStats, InBackpack, RunState, State, Viewshed};
use crate::gamelog::GameLog; use crate::gamelog::GameLog;
use crate::Map; use crate::Map;
use crate::Name; use crate::Name;
use crate::Player; use crate::Player;
use crate::Position; use crate::Position;
use crate::{CombatStats, InBackpack, RunState, State, Viewshed};
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) { pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
ctx.draw_box( ctx.draw_box(
@ -413,7 +413,6 @@ pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
"Rust Roguelike Tutorial", "Rust Roguelike Tutorial",
); );
if let RunState::MainMenu { if let RunState::MainMenu {
menu_selection: selection, menu_selection: selection,
} = *runstate } = *runstate
@ -452,7 +451,6 @@ pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
} }
} }
if selection == MainMenuSelection::Quit { if selection == MainMenuSelection::Quit {
ctx.print_color_centered( ctx.print_color_centered(
26, 26,

View File

@ -26,9 +26,9 @@ mod melee_combat_system;
mod monster_ai_system; mod monster_ai_system;
mod player; mod player;
mod rect; mod rect;
mod save_load_system;
mod spawner; mod spawner;
mod visibility_system; mod visibility_system;
mod save_load_system;
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
pub enum RunState { pub enum RunState {

View File

@ -1,6 +1,6 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, RGB, Rltk}; use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use specs::prelude::*; use specs::prelude::*;
@ -65,7 +65,6 @@ impl Map {
} }
} }
fn is_exit_valid(&self, x: i32, y: i32) -> bool { fn is_exit_valid(&self, x: i32, y: i32) -> bool {
if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 { if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
return false; return false;

View File

@ -3,8 +3,8 @@ use specs::prelude::*;
use crate::{ use crate::{
components::{Monster, Position, Viewshed, WantsToMelee}, components::{Monster, Position, Viewshed, WantsToMelee},
Confusion, map::Map,
map::Map, RunState, Confusion, RunState,
}; };
pub struct MonsterAI {} pub struct MonsterAI {}
@ -43,7 +43,7 @@ impl<'a> System<'a> for MonsterAI {
} }
for (entity, mut viewshed, _monster, mut pos) in for (entity, mut viewshed, _monster, mut pos) in
(&entities, &mut viewshed, &monster, &mut position).join() (&entities, &mut viewshed, &monster, &mut position).join()
{ {
let mut can_act = true; let mut can_act = true;

View File

@ -3,12 +3,12 @@ use std::cmp::{max, min};
use rltk::{Point, Rltk, VirtualKeyCode}; use rltk::{Point, Rltk, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
use crate::gamelog::GameLog;
use crate::{ use crate::{
components::{CombatStats, Player, Position, Viewshed, WantsToMelee}, components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
Item, map::Map,
map::Map, RunState, State, WantsToPickupItem, Item, RunState, State, WantsToPickupItem,
}; };
use crate::gamelog::GameLog;
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>(); let mut positions = ecs.write_storage::<Position>();
@ -20,9 +20,15 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let map = ecs.fetch::<Map>(); let map = ecs.fetch::<Map>();
for (entity, _player, pos, viewshed) in for (entity, _player, pos, viewshed) in
(&entities, &mut players, &mut positions, &mut viewsheds).join() (&entities, &mut players, &mut positions, &mut viewsheds).join()
{ {
if pos.x + delta_x < 1 || pos.x + delta_x > map.width - 1 || pos.y + delta_y < 1 || pos.y + delta_y > map.height - 1 { return; } if pos.x + delta_x < 1
|| pos.x + delta_x > map.width - 1
|| pos.y + delta_y < 1
|| pos.y + delta_y > map.height - 1
{
return;
}
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
for potential_target in map.tile_content[destination_idx].iter() { for potential_target in map.tile_content[destination_idx].iter() {

View File

@ -4,7 +4,9 @@ use std::path::Path;
use specs::error::NoError; use specs::error::NoError;
use specs::prelude::*; use specs::prelude::*;
use specs::saveload::{DeserializeComponents, MarkedBuilder, SerializeComponents, SimpleMarker, SimpleMarkerAllocator}; use specs::saveload::{
DeserializeComponents, MarkedBuilder, SerializeComponents, SimpleMarker, SimpleMarkerAllocator,
};
use super::components::*; use super::components::*;
@ -37,14 +39,39 @@ pub fn save_game(ecs: &mut World) {
// Actually serialize // Actually serialize
{ {
let data = (ecs.entities(), ecs.read_storage::<SimpleMarker<SerializeMe>>()); let data = (
ecs.entities(),
ecs.read_storage::<SimpleMarker<SerializeMe>>(),
);
let writer = File::create("./savegame.json").unwrap(); let writer = File::create("./savegame.json").unwrap();
let mut serializer = serde_json::Serializer::new(writer); let mut serializer = serde_json::Serializer::new(writer);
serialize_individually!(ecs, serializer, data, Position, Renderable, Player, Viewshed, Monster, serialize_individually!(
Name, BlocksTile, CombatStats, SufferDamage, WantsToMelee, Item, Consumable, Ranged, InflictsDamage, ecs,
AreaOfEffect, Confusion, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem, serializer,
WantsToDropItem, SerializationHelper data,
Position,
Renderable,
Player,
Viewshed,
Monster,
Name,
BlocksTile,
CombatStats,
SufferDamage,
WantsToMelee,
Item,
Consumable,
Ranged,
InflictsDamage,
AreaOfEffect,
Confusion,
ProvidesHealing,
InBackpack,
WantsToPickupItem,
WantsToUseItem,
WantsToDropItem,
SerializationHelper
); );
} }
@ -87,12 +114,38 @@ pub fn load_game(ecs: &mut World) {
let mut de = serde_json::Deserializer::from_str(&data); let mut de = serde_json::Deserializer::from_str(&data);
{ {
let mut d = (&mut ecs.entities(), &mut ecs.write_storage::<SimpleMarker<SerializeMe>>(), &mut ecs.write_resource::<SimpleMarkerAllocator<SerializeMe>>()); let mut d = (
&mut ecs.entities(),
&mut ecs.write_storage::<SimpleMarker<SerializeMe>>(),
&mut ecs.write_resource::<SimpleMarkerAllocator<SerializeMe>>(),
);
deserialize_individually!(ecs, de, d, Position, Renderable, Player, Viewshed, Monster, deserialize_individually!(
Name, BlocksTile, CombatStats, SufferDamage, WantsToMelee, Item, Consumable, Ranged, InflictsDamage, ecs,
AreaOfEffect, Confusion, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem, de,
WantsToDropItem, SerializationHelper d,
Position,
Renderable,
Player,
Viewshed,
Monster,
Name,
BlocksTile,
CombatStats,
SufferDamage,
WantsToMelee,
Item,
Consumable,
Ranged,
InflictsDamage,
AreaOfEffect,
Confusion,
ProvidesHealing,
InBackpack,
WantsToPickupItem,
WantsToUseItem,
WantsToDropItem,
SerializationHelper
); );
} }
@ -115,9 +168,12 @@ pub fn load_game(ecs: &mut World) {
*player_resource = e; *player_resource = e;
} }
} }
ecs.delete_entity(deleteme.unwrap()).expect("Unable to delete helper"); ecs.delete_entity(deleteme.unwrap())
.expect("Unable to delete helper");
} }
pub fn delete_save() { pub fn delete_save() {
if Path::new("./savegame.json").exists() { std::fs::remove_file("./savegame.json").expect("Unable to delete file"); } if Path::new("./savegame.json").exists() {
std::fs::remove_file("./savegame.json").expect("Unable to delete file");
}
} }

View File

@ -2,8 +2,12 @@ use rltk::{FontCharType, RandomNumberGenerator, RGB};
use specs::prelude::*; use specs::prelude::*;
use specs::saveload::{MarkedBuilder, SimpleMarker}; use specs::saveload::{MarkedBuilder, SimpleMarker};
use crate::{AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, InflictsDamage, Item, MAP_WIDTH, MAX_ITEMS, MAX_MONSTER, Monster, Name, Player, Position, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed};
use crate::rect::Rect; use crate::rect::Rect;
use crate::{
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, InflictsDamage, Item, Monster,
Name, Player, Position, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed, MAP_WIDTH,
MAX_ITEMS, MAX_MONSTER,
};
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
ecs.create_entity() ecs.create_entity()