Compare commits
35 Commits
6d7da45b18
...
map-builde
Author | SHA1 | Date | |
---|---|---|---|
841a83acb7
|
|||
d2b800328b
|
|||
1fca99ae3c | |||
d018a1bd3c
|
|||
7cfb253483
|
|||
97c7a45670
|
|||
c926b8d05a
|
|||
5f9f15b2af
|
|||
cbf9e47ff8
|
|||
7b56c52fff
|
|||
6a2c77404b
|
|||
aa7e888d9e
|
|||
170088b836
|
|||
34d1de5847
|
|||
70e4ac0c81
|
|||
ef4a98627e
|
|||
e816846d19
|
|||
9e16c22eb5
|
|||
66854be921
|
|||
04fe103b25
|
|||
4ab62ea921
|
|||
32f6a822a2
|
|||
89ddbd2483
|
|||
b7b89b801b
|
|||
3fe46fd116
|
|||
bc5eb9b006
|
|||
6705870f81
|
|||
e863f5af88
|
|||
c8384883ad
|
|||
11dc9771cc
|
|||
6791c0cf48
|
|||
ad8852f4b8
|
|||
bfa4d87ece
|
|||
0ac3e87765
|
|||
9a38783859
|
22
.cargo/config.toml
Normal file
22
.cargo/config.toml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below.
|
||||||
|
|
||||||
|
# NOTE: For maximum performance, build using a nightly compiler
|
||||||
|
# If you are using rust stable, remove the "-Zshare-generics=y" below.
|
||||||
|
|
||||||
|
[target.x86_64-unknown-linux-gnu]
|
||||||
|
linker = "/usr/bin/clang"
|
||||||
|
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"]
|
||||||
|
|
||||||
|
# NOTE: you must manually install https://github.com/michaeleisel/zld on mac. you can easily do this with the "brew" package manager:
|
||||||
|
# `brew install michaeleisel/zld/zld`
|
||||||
|
[target.x86_64-apple-darwin]
|
||||||
|
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld", "-Zshare-generics=y"]
|
||||||
|
|
||||||
|
[target.x86_64-pc-windows-msvc]
|
||||||
|
linker = "rust-lld.exe"
|
||||||
|
rustflags = ["-Zshare-generics=y"]
|
||||||
|
|
||||||
|
# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
|
||||||
|
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
|
||||||
|
#[profile.dev]
|
||||||
|
#debug = 1
|
3469
Cargo.lock
generated
Normal file
3469
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
14
Cargo.toml
@ -1,8 +1,22 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "adventure-game"
|
name = "adventure-game"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
authors = ["Daniel Lynn <daniel.eric.lynn@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bevy = { version = "0.5", features = ["dynamic"] }
|
||||||
|
rand = "0.8"
|
||||||
|
getrandom = { version = "0.2", features = ["wasm-bindgen"] }
|
||||||
|
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 3
|
||||||
|
|
||||||
|
[profile.dev]
|
||||||
|
opt-level = 1
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["hot-reload"]
|
||||||
|
hot-reload = []
|
||||||
|
BIN
assets/default_tilemap.aseprite
Normal file
BIN
assets/default_tilemap.aseprite
Normal file
Binary file not shown.
BIN
assets/grass.aseprite
Normal file
BIN
assets/grass.aseprite
Normal file
Binary file not shown.
BIN
assets/grass.png
Normal file
BIN
assets/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 153 B |
BIN
assets/marshmallow.aseprite
Normal file
BIN
assets/marshmallow.aseprite
Normal file
Binary file not shown.
BIN
assets/marshmallow.png
Normal file
BIN
assets/marshmallow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 335 B |
BIN
assets/wall.aseprite
Normal file
BIN
assets/wall.aseprite
Normal file
Binary file not shown.
BIN
assets/wall.png
Normal file
BIN
assets/wall.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 624 B |
2
rust-toolchain
Normal file
2
rust-toolchain
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
2
src/components/mod.rs
Normal file
2
src/components/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub struct Name(pub String);
|
||||||
|
pub struct Player;
|
41
src/main.rs
41
src/main.rs
@ -1,3 +1,40 @@
|
|||||||
fn main() {
|
mod components;
|
||||||
println!("Hello, world!");
|
mod map;
|
||||||
|
mod plugins;
|
||||||
|
mod resources;
|
||||||
|
mod systems;
|
||||||
|
|
||||||
|
mod prelude {
|
||||||
|
pub use crate::components::*;
|
||||||
|
pub use crate::map::*;
|
||||||
|
pub use crate::plugins::*;
|
||||||
|
pub use crate::resources::*;
|
||||||
|
pub use crate::systems::*;
|
||||||
|
pub use bevy::{
|
||||||
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
pub use rand::prelude::*;
|
||||||
|
|
||||||
|
pub const MAP_WIDTH: i32 = 80;
|
||||||
|
pub const MAP_HEIGHT: i32 = 50;
|
||||||
|
pub const CELL_SIZE: i32 = 64;
|
||||||
|
pub const NUM_TILES: usize = (MAP_WIDTH * MAP_HEIGHT) as usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
use prelude::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
App::build()
|
||||||
|
.insert_resource(ClearColor(Color::rgb(0.171875, 0.171875, 0.171875)))
|
||||||
|
.init_resource::<Map>()
|
||||||
|
.add_plugins(DefaultPlugins)
|
||||||
|
//.add_plugin(FrameTimeDiagnosticsPlugin)
|
||||||
|
//.add_plugin(LogDiagnosticsPlugin)
|
||||||
|
//.add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::< ColorMaterial>)
|
||||||
|
.add_startup_system(setup.system())
|
||||||
|
.add_startup_system(add_player.system())
|
||||||
|
.add_system(player_movement.system())
|
||||||
|
.add_system(follow_player.system())
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
|
25
src/map.rs
Normal file
25
src/map.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum Tile {
|
||||||
|
Floor,
|
||||||
|
Wall,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Map {
|
||||||
|
pub tiles: Vec<Tile>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Map {
|
||||||
|
pub fn index(&self, x: i32, y: i32) -> usize {
|
||||||
|
((y * MAP_HEIGHT) + x) as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Map {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
tiles: vec![Tile::Floor; NUM_TILES],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
src/plugins/mod.rs
Normal file
11
src/plugins/mod.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct PlayerPlugin;
|
||||||
|
|
||||||
|
impl Plugin for PlayerPlugin {
|
||||||
|
fn build(&self, app: &mut AppBuilder) {
|
||||||
|
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, true)))
|
||||||
|
.add_startup_system(add_player.system())
|
||||||
|
.add_system(greet_player.system());
|
||||||
|
}
|
||||||
|
}
|
3
src/resources/mod.rs
Normal file
3
src/resources/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct GreetTimer(pub Timer);
|
118
src/systems/mod.rs
Normal file
118
src/systems/mod.rs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use bevy::render::camera::Camera;
|
||||||
|
|
||||||
|
pub fn setup(
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
|
map: Res<Map>,
|
||||||
|
) {
|
||||||
|
// Camera
|
||||||
|
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
||||||
|
|
||||||
|
// Map
|
||||||
|
let grass_material = materials.add(asset_server.load("grass.png").into());
|
||||||
|
let wall_material = materials.add(asset_server.load("wall.png").into());
|
||||||
|
let half_w = MAP_WIDTH * CELL_SIZE / 2;
|
||||||
|
let half_h = MAP_HEIGHT * CELL_SIZE / 2;
|
||||||
|
|
||||||
|
for j in 0..MAP_HEIGHT {
|
||||||
|
let y = j * CELL_SIZE - half_h;
|
||||||
|
for i in 0..MAP_WIDTH {
|
||||||
|
let x = i * CELL_SIZE - half_w;
|
||||||
|
let index = map.index(i, j);
|
||||||
|
let tile = map.tiles[index];
|
||||||
|
let material = match tile {
|
||||||
|
Tile::Floor => grass_material.clone(),
|
||||||
|
Tile::Wall => wall_material.clone(),
|
||||||
|
};
|
||||||
|
let sprite = Sprite::new(Vec2::new(CELL_SIZE as f32, CELL_SIZE as f32));
|
||||||
|
let transform = Transform::from_xyz(x as f32, y as f32, 0.0);
|
||||||
|
|
||||||
|
commands.spawn_bundle(SpriteBundle {
|
||||||
|
material,
|
||||||
|
sprite,
|
||||||
|
transform,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_player(
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
|
) {
|
||||||
|
let player_material = materials.add(asset_server.load("marshmallow.png").into());
|
||||||
|
|
||||||
|
commands
|
||||||
|
.spawn()
|
||||||
|
.insert(Player)
|
||||||
|
.insert(Name("Player 1".to_string()))
|
||||||
|
.insert_bundle(SpriteBundle {
|
||||||
|
material: player_material,
|
||||||
|
sprite: Sprite::new(Vec2::new(CELL_SIZE as f32, CELL_SIZE as f32)),
|
||||||
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn player_movement(
|
||||||
|
keyboard_input: Res<Input<KeyCode>>,
|
||||||
|
mut query: Query<(&Player, &mut Transform)>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
if let Ok((_, mut transform)) = query.single_mut() {
|
||||||
|
let mut direction = Vec2::ZERO;
|
||||||
|
|
||||||
|
if keyboard_input.pressed(KeyCode::A) {
|
||||||
|
direction.x -= 1.0;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::D) {
|
||||||
|
direction.x += 1.0;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::S) {
|
||||||
|
direction.y -= 1.0;
|
||||||
|
}
|
||||||
|
if keyboard_input.pressed(KeyCode::W) {
|
||||||
|
direction.y += 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let translation = &mut transform.translation;
|
||||||
|
let delta_seconds = time.delta_seconds();
|
||||||
|
|
||||||
|
translation.x += delta_seconds * direction.x * 300.0;
|
||||||
|
translation.y += delta_seconds * direction.y * 300.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_player(
|
||||||
|
mut q: QuerySet<(
|
||||||
|
Query<&Transform, With<Player>>,
|
||||||
|
Query<&mut Transform, With<Camera>>,
|
||||||
|
)>,
|
||||||
|
) {
|
||||||
|
if let Ok(player_transform) = q.q0().single() {
|
||||||
|
let player_translation = player_transform.translation;
|
||||||
|
|
||||||
|
if let Ok(mut camera_transform) = q.q1_mut().single_mut() {
|
||||||
|
let camera_translation = &mut camera_transform.translation;
|
||||||
|
|
||||||
|
camera_translation.x = player_translation.x;
|
||||||
|
camera_translation.y = player_translation.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn greet_player(
|
||||||
|
query: Query<&Name, With<Player>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
mut timer: ResMut<GreetTimer>,
|
||||||
|
) {
|
||||||
|
if timer.0.tick(time.delta()).just_finished() {
|
||||||
|
for name in query.iter() {
|
||||||
|
println!("Hello, {}!", name.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user