0239-RLTK-分割不同文件

环境

  • Time 2022-11-30
  • WSL-Ubuntu 22.04
  • RLTK 0.8.7

前言

说明

参考:https://bfnightly.bracketproductions.com/rustbook

目标

基于前一节的内容,随着 main.rs 文件中的内容越来越多,将其进行分割。

comp.rs

use rltk::RGB;
use specs::prelude::*;
use specs_derive::Component;

#[derive(Component)]
pub struct Position {
    pub x: i32,
    pub y: i32,
}

#[derive(Component)]
pub struct Renderable {
    pub glyph: rltk::FontCharType,
    pub fg: RGB,
    pub bg: RGB,
}

#[derive(Component)]
pub struct LeftWalker {}

#[derive(Component, Debug)]
pub struct Player {}

impl<'a> System<'a> for LeftWalker {
    type SystemData = (ReadStorage<'a, LeftWalker>, WriteStorage<'a, Position>);

    fn run(&mut self, (lefty, mut pos): Self::SystemData) {
        for (_lefty, pos) in (&lefty, &mut pos).join() {
            pos.x -= 1;
            if pos.x < 0 {
                pos.x = 79;
            }
        }
    }
}

player.rs

use rltk::prelude::*;
use specs::{Join, World, WorldExt};
use std::cmp::{max, min};

use crate::comp::{Player, Position};
use crate::map::{self, TileType};
use crate::State;

pub fn move_player(delta_x: i32, delta_y: i32, world: &mut World) {
    let mut positions = world.write_storage::<Position>();
    let mut players = world.write_storage::<Player>();
    let map = world.fetch::<Vec<TileType>>();

    for (_player, pos) in (&mut players, &mut positions).join() {
        let destination_idx = map::index(pos.x + delta_x, pos.y + delta_y);
        if map[destination_idx] != TileType::Wall {
            pos.x = min(79, max(0, pos.x + delta_x));
            pos.y = min(49, max(0, pos.y + delta_y));
        }
    }
}

pub fn player_input(state: &mut State, context: &mut Rltk) {
    if let Some(key) = context.key {
        match key {
            VirtualKeyCode::Left => move_player(-1, 0, &mut state.world),
            VirtualKeyCode::Right => move_player(1, 0, &mut state.world),
            VirtualKeyCode::Up => move_player(0, -1, &mut state.world),
            VirtualKeyCode::Down => move_player(0, 1, &mut state.world),
            _ => {}
        }
    }
}

map.rs

use rltk::prelude::*;

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum TileType {
    Wall,
    Floor,
}

pub fn index(x: i32, y: i32) -> usize {
    (y as usize * 80) + x as usize
}

pub fn new_map() -> Vec<TileType> {
    let mut map = vec![TileType::Floor; 80 * 50];

    for x in 0..80 {
        map[index(x, 0)] = TileType::Wall;
        map[index(x, 49)] = TileType::Wall;
    }
    for y in 0..50 {
        map[index(0, y)] = TileType::Wall;
        map[index(79, y)] = TileType::Wall;
    }

    let mut rng = rltk::RandomNumberGenerator::new();

    for _i in 0..400 {
        let x = rng.roll_dice(1, 79);
        let y = rng.roll_dice(1, 49);
        let idx = index(x, y);
        if idx != index(40, 25) {
            map[idx] = TileType::Wall;
        }
    }

    map
}

pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
    let mut y = 0;
    let mut x = 0;
    for tile in map.iter() {
        match tile {
            TileType::Floor => {
                ctx.set(
                    x,
                    y,
                    RGB::from_f32(0.5, 0.5, 0.5),
                    RGB::from_f32(0., 0., 0.),
                    rltk::to_cp437('.'),
                );
            }
            TileType::Wall => {
                ctx.set(
                    x,
                    y,
                    RGB::from_f32(0.0, 1.0, 0.0),
                    RGB::from_f32(0., 0., 0.),
                    rltk::to_cp437('#'),
                );
            }
        }

        x += 1;
        if x > 79 {
            x = 0;
            y += 1;
        }
    }
}

main.rs

use comp::{LeftWalker, Player, Position, Renderable};
use map::TileType;
use rltk::prelude::*;
use specs::prelude::*;

mod comp;
mod map;
mod player;

pub struct State {
    world: World,
}
impl GameState for State {
    fn tick(&mut self, context: &mut Rltk) {
        context.cls();

        player::player_input(self, context);
        self.run_systems();

        let map1 = self.world.fetch::<Vec<TileType>>();
        map::draw_map(&map1, context);

        let positions = self.world.read_storage::<Position>();
        let renderables = self.world.read_storage::<Renderable>();
        for (pos, render) in (&positions, &renderables).join() {
            context.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
        }
    }
}

impl State {
    fn run_systems(&mut self) {
        let mut lw = LeftWalker {};
        lw.run_now(&self.world);
        self.world.maintain();
    }
}

fn main() -> rltk::BError {
    let context = rltk::RltkBuilder::simple80x50()
        .with_title("冒险游戏")
        .build()?;

    let mut state = State {
        world: World::new(),
    };
    state.world.register::<Position>();
    state.world.register::<Renderable>();
    state.world.register::<LeftWalker>();
    state.world.register::<Player>();

    state.world.insert(map::new_map());

    state
        .world
        .create_entity()
        .with(Position { x: 40, y: 25 })
        .with(Renderable {
            glyph: rltk::to_cp437('@'),
            fg: RGB::named(rltk::YELLOW),
            bg: RGB::named(rltk::BLACK),
        })
        .with(Player {})
        .build();

    rltk::main_loop(context, state)
}

效果

生成地图

总结

将功能划分到了不同的文件,并且保持功能不变。

附录

posted @   jiangbo4444  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-08-16 【JavaScript】Boolean
点击右上角即可分享
微信分享提示