123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- extern crate rltk;
- use rltk::{Console, GameState, Rltk, RGB, Point};
- extern crate specs;
- use specs::prelude::*;
- #[macro_use]
- extern crate specs_derive;
- mod components;
- pub use components::*;
- mod map;
- pub use map::*;
- mod player;
- use player::*;
- mod rect;
- pub use rect::Rect;
- mod visibility_system;
- use visibility_system::VisibilitySystem;
- mod monster_ai_system;
- use monster_ai_system::MonsterAI;
- mod map_indexing_system;
- use map_indexing_system::MapIndexingSystem;
- mod melee_combat_system;
- use melee_combat_system::MeleeCombatSystem;
- mod damage_system;
- use damage_system::DamageSystem;
- mod gui;
- mod gamelog;
- #[derive(PartialEq, Copy, Clone)]
- pub enum RunState { AwaitingInput, PreRun, PlayerTurn, MonsterTurn }
- pub struct State {
- pub ecs: World,
- pub systems: Dispatcher<'static, 'static>
- }
- impl GameState for State {
- fn tick(&mut self, ctx : &mut Rltk) {
- ctx.cls();
- let mut newrunstate;
- {
- let runstate = self.ecs.fetch::<RunState>();
- newrunstate = *runstate;
- }
-
- match newrunstate {
- RunState::PreRun => {
- self.systems.dispatch(&self.ecs);
- newrunstate = RunState::AwaitingInput;
- }
- RunState::AwaitingInput => {
- newrunstate = player_input(self, ctx);
- }
- RunState::PlayerTurn => {
- self.systems.dispatch(&self.ecs);
- newrunstate = RunState::MonsterTurn;
- }
- RunState::MonsterTurn => {
- self.systems.dispatch(&self.ecs);
- newrunstate = RunState::AwaitingInput;
- }
- }
- {
- let mut runwriter = self.ecs.write_resource::<RunState>();
- *runwriter = newrunstate;
- }
- damage_system::delete_the_dead(&mut self.ecs);
- draw_map(&self.ecs, ctx);
- let positions = self.ecs.read_storage::<Position>();
- let renderables = self.ecs.read_storage::<Renderable>();
- let map = self.ecs.fetch::<Map>();
- for (pos, render) in (&positions, &renderables).join() {
- let idx = map.xy_idx(pos.x, pos.y);
- if map.visible_tiles[idx] { ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph) }
- }
- gui::draw_ui(&self.ecs, ctx);
- }
- }
- fn main() {
- let mut context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
- context.with_post_scanlines(true);
- let mut gs = State {
- ecs: World::new(),
- systems : DispatcherBuilder::new()
- .with(MapIndexingSystem{}, "map_indexing_system", &[])
- .with(VisibilitySystem{}, "visibility_system", &[])
- .with(MonsterAI{}, "monster_ai", &["visibility_system", "map_indexing_system"])
- .with(MeleeCombatSystem{}, "melee_combat", &["monster_ai"])
- .with(DamageSystem{}, "damage", &["melee_combat"])
- .build(),
- };
- gs.ecs.register::<Position>();
- gs.ecs.register::<Renderable>();
- gs.ecs.register::<Player>();
- gs.ecs.register::<Viewshed>();
- gs.ecs.register::<Monster>();
- gs.ecs.register::<Name>();
- gs.ecs.register::<BlocksTile>();
- gs.ecs.register::<CombatStats>();
- gs.ecs.register::<WantsToMelee>();
- gs.ecs.register::<SufferDamage>();
- let map : Map = Map::new_map_rooms_and_corridors();
- let (player_x, player_y) = map.rooms[0].center();
- let player_entity = gs.ecs
- .create_entity()
- .with(Position { x: player_x, y: player_y })
- .with(Renderable {
- glyph: rltk::to_cp437('@'),
- fg: RGB::named(rltk::YELLOW),
- bg: RGB::named(rltk::BLACK),
- })
- .with(Player{})
- .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
- .with(Name{name: "Player".to_string() })
- .with(CombatStats{ max_hp: 30, hp: 30, defense: 2, power: 5 })
- .build();
- let mut rng = rltk::RandomNumberGenerator::new();
- for (i,room) in map.rooms.iter().skip(1).enumerate() {
- let (x,y) = room.center();
- let glyph : u8;
- let name : String;
- let roll = rng.roll_dice(1, 2);
- match roll {
- 1 => { glyph = rltk::to_cp437('g'); name = "Goblin".to_string(); }
- _ => { glyph = rltk::to_cp437('o'); name = "Orc".to_string(); }
- }
- gs.ecs.create_entity()
- .with(Position{ x, y })
- .with(Renderable{
- glyph,
- fg: RGB::named(rltk::RED),
- bg: RGB::named(rltk::BLACK),
- })
- .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
- .with(Monster{})
- .with(Name{ name: format!("{} #{}", &name, i) })
- .with(BlocksTile{})
- .with(CombatStats{ max_hp: 16, hp: 16, defense: 1, power: 4 })
- .build();
- }
- gs.ecs.insert(map);
- gs.ecs.insert(Point::new(player_x, player_y));
- gs.ecs.insert(player_entity);
- gs.ecs.insert(RunState::PreRun);
- gs.ecs.insert(gamelog::GameLog{ entries : vec!["Welcome to Rusty Roguelike".to_string()] });
- rltk::main_loop(context, gs);
- }
|