This commit is contained in:
Daniel Flanagan 2024-08-02 13:40:43 -05:00
parent 4d5f9be124
commit 46e125da03
6 changed files with 196 additions and 36 deletions

54
Cargo.lock generated
View file

@ -575,6 +575,17 @@ dependencies = [
"syn 2.0.72", "syn 2.0.72",
] ]
[[package]]
name = "bevy_ecs_tilemap"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d880047f5deaf5166ffc08238125a4ccbd2837f781ca6525fa200fcf5785ba3b"
dependencies = [
"bevy",
"log",
"regex",
]
[[package]] [[package]]
name = "bevy_egui" name = "bevy_egui"
version = "0.28.0" version = "0.28.0"
@ -2258,7 +2269,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy-inspector-egui", "bevy-inspector-egui",
"bevy_ecs_tilemap",
"bevy_framepace", "bevy_framepace",
"rand",
"rand_pcg",
"rand_seeder",
] ]
[[package]] [[package]]
@ -2984,6 +2999,12 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "presser" name = "presser"
version = "0.3.1" version = "0.3.1"
@ -3064,6 +3085,18 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [ dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core", "rand_core",
] ]
@ -3072,6 +3105,27 @@ name = "rand_core"
version = "0.6.4" version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_pcg"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_seeder"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a9febe641d2842ffc76ee962668a17578767c4e01735e4802b21ed9a24b2e4e"
dependencies = [
"rand_core",
]
[[package]] [[package]]
name = "range-alloc" name = "range-alloc"

View file

@ -37,7 +37,11 @@ bevy = { version = "0.14.0", default-features = false, features = [
# "detailed_trace", # probably useful troubleshooting information during runtime # "detailed_trace", # probably useful troubleshooting information during runtime
] } ] }
bevy-inspector-egui = "0.25.1" bevy-inspector-egui = "0.25.1"
bevy_ecs_tilemap = "0.14.0"
bevy_framepace = "0.17.1" bevy_framepace = "0.17.1"
rand = "0.8.5"
rand_pcg = "0.3.1"
rand_seeder = "0.3.0"
[profile.dev] [profile.dev]
opt-level = 1 opt-level = 1

BIN
assets/img/tiles.ase Normal file

Binary file not shown.

View file

@ -2,6 +2,7 @@ mod camera;
mod game; mod game;
mod input; mod input;
mod main_menu; mod main_menu;
mod map;
mod movement; mod movement;
mod player; mod player;
mod prelude; mod prelude;
@ -10,6 +11,7 @@ mod statue;
use bevy::asset::load_internal_binary_asset; use bevy::asset::load_internal_binary_asset;
use bevy::audio::{AudioPlugin, SpatialScale}; use bevy::audio::{AudioPlugin, SpatialScale};
use bevy::window::{PrimaryWindow, WindowMode}; use bevy::window::{PrimaryWindow, WindowMode};
use bevy_ecs_tilemap::TilemapPlugin;
use input::Input; use input::Input;
use prelude::*; use prelude::*;
use statue::SpawnStatueEvent; use statue::SpawnStatueEvent;
@ -24,8 +26,9 @@ pub enum Game {
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)] #[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
pub enum View { pub enum View {
#[default] #[default]
LoadingScreen, LoadingMenu,
MainMenu, MainMenu,
LoadingGame,
InGame, InGame,
} }
@ -60,7 +63,8 @@ fn main() -> AppExit {
.set(ImagePlugin::default_nearest()), .set(ImagePlugin::default_nearest()),
bevy::diagnostic::FrameTimeDiagnosticsPlugin, bevy::diagnostic::FrameTimeDiagnosticsPlugin,
bevy_framepace::FramepacePlugin, bevy_framepace::FramepacePlugin,
bevy_inspector_egui::quick::WorldInspectorPlugin::new(), // bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
TilemapPlugin,
)); ));
load_internal_binary_asset!( load_internal_binary_asset!(
@ -91,45 +95,58 @@ fn main() -> AppExit {
app.add_event::<SpawnStatueEvent>(); app.add_event::<SpawnStatueEvent>();
app.add_systems(OnEnter(View::MainMenu), main_menu::startup) app.add_systems(
.add_systems(OnExit(View::MainMenu), main_menu::exit) OnEnter(View::LoadingMenu),
.add_systems(OnEnter(View::InGame), (player::startup, statue::startup)) ((startup, camera::startup), main_menu::startup),
.add_systems(OnExit(View::InGame), (player::exit, statue::exit)) )
.add_systems(OnEnter(View::LoadingScreen), (startup, camera::startup)) .add_systems(OnExit(View::MainMenu), main_menu::exit)
.add_systems( .add_systems(
Update, OnEnter(View::LoadingGame),
(player::startup, statue::startup, map::spawn),
)
.add_systems(
OnExit(View::InGame),
(player::exit, statue::exit, map::exit),
)
.add_systems(
Update,
(
load_menu.run_if(in_state(View::LoadingMenu)),
load_game.run_if(in_state(View::LoadingGame)),
input::process_input,
(update,).after(input::process_input),
( (
finish_setup.run_if(in_state(View::LoadingScreen)), player::control,
input::process_input, player::meow_on_r,
(update,).after(input::process_input), player::player_debug_info,
( statue::spawn_statue,
player::control, movement::update_velocity_by_heading,
player::meow_on_r, movement::resolve_velocity.after(movement::update_velocity_by_heading),
player::player_debug_info, movement::ysort.after(movement::update_velocity_by_heading),
statue::spawn_statue, camera::update
movement::update_velocity_by_heading, .after(player::control)
movement::resolve_velocity.after(movement::update_velocity_by_heading), .after(movement::resolve_velocity),
movement::ysort.after(movement::update_velocity_by_heading), )
camera::update .after(input::process_input)
.after(player::control) .in_set(InGameSet),
.after(movement::resolve_velocity), (main_menu::update).in_set(MainMenuSet),
) ),
.after(input::process_input) )
.in_set(InGameSet), .insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5)));
(main_menu::update).in_set(MainMenuSet),
),
)
.insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5)));
app.run() app.run()
} }
#[derive(Component)] #[derive(Component)]
struct FpsText; struct FpsText;
fn finish_setup(mut app_state: ResMut<NextState<View>>) { fn load_menu(mut app_state: ResMut<NextState<View>>) {
app_state.set(View::MainMenu); app_state.set(View::MainMenu);
} }
fn load_game(mut app_state: ResMut<NextState<View>>) {
app_state.set(View::InGame);
}
fn startup(mut commands: Commands) { fn startup(mut commands: Commands) {
let font_size = 48.; let font_size = 48.;
commands.spawn(( commands.spawn((
@ -144,7 +161,6 @@ fn startup(mut commands: Commands) {
), ),
TextSection::from_style(TextStyle { TextSection::from_style(TextStyle {
font_size, font_size,
color: Color::hsla(0.5, 0.5, 0.5, 0.5),
..default() ..default()
}), }),
]) ])
@ -170,20 +186,22 @@ fn update(
if input.cancel { if input.cancel {
match view.get() { match view.get() {
View::InGame => { View::InGame => {
next_state.set(crate::View::MainMenu); next_state.set(crate::View::LoadingMenu);
} }
View::MainMenu => { View::MainMenu => {
app_exit_events.send(AppExit::Success); app_exit_events.send(AppExit::Success);
} }
View::LoadingScreen => {} View::LoadingMenu => {}
View::LoadingGame => {}
} }
} else if input.action { } else if input.action {
match view.get() { match view.get() {
View::InGame => {} View::InGame => {}
View::MainMenu => { View::MainMenu => {
next_state.set(crate::View::InGame); next_state.set(crate::View::LoadingGame);
} }
View::LoadingScreen => {} View::LoadingMenu => {}
View::LoadingGame => {}
} }
} }
if keys.just_pressed(KeyCode::Enter) if keys.just_pressed(KeyCode::Enter)

82
src/map.rs Normal file
View file

@ -0,0 +1,82 @@
use crate::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use rand::prelude::*;
use rand_pcg::Pcg64;
use rand_seeder::Seeder;
#[derive(Component, Debug)]
pub struct Tilemap;
pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
// let task_pool = AsyncComputeTaskPool::get();
// let task = task_pool.spawn(async move {});
let mut rng: Pcg64 = Seeder::from("stripy zebra").make_rng();
let size = TilemapSize { x: 1024, y: 1024 };
let tilemap_entity = commands.spawn_empty().id();
let mut tile_storage = TileStorage::empty(size);
let mut batch = Vec::<TileBundle>::with_capacity(size.x as usize * size.y as usize);
for x in 0..size.x {
for y in 0..size.y {
let tile_pos = TilePos { x, y };
let texture_index = TileTextureIndex(if rng.gen_range(0..1000) > 925 {
rng.gen_range(0..(16 * 8))
} else {
0
});
// let texture_index = TileTextureIndex(0);
// let tile_entity = commands
// .spawn(TileBundle {
// position: tile_pos,
// tilemap_id: TilemapId(tilemap_entity),
// texture_index,
// ..Default::default()
// })
// .id();
// let tile_entity = commands
batch.push(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index,
..Default::default()
});
// .id();
// tile_storage.set(&tile_pos, tile_entity);
}
}
commands.spawn_batch(batch);
// for tb in batch {
// tile_storage.set(&tile_pos, tile_entity);
// }
let texture_handle: Handle<Image> = assets.load("img/Tileset Grass.png");
let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();
let map_type = TilemapType::default();
let texture = TilemapTexture::Single(texture_handle);
commands.entity(tilemap_entity).insert((
Tilemap,
TilemapBundle {
grid_size,
map_type,
size,
storage: tile_storage,
texture,
tile_size,
transform: get_tilemap_center_transform(&size, &grid_size, &map_type, f32::MIN),
..default()
},
));
}
pub fn exit(mut commands: Commands, q: Query<Entity, With<Tilemap>>) {
for id in q.iter() {
commands.entity(id).despawn_recursive();
}
}

View file

@ -41,6 +41,7 @@ pub fn startup(
commands commands
.spawn(( .spawn((
Player, Player,
Name::new("Player"),
VisibilityBundle { VisibilityBundle {
visibility: Visibility::Inherited, visibility: Visibility::Inherited,
..default() ..default()
@ -57,6 +58,7 @@ pub fn startup(
)) ))
.with_children(|player| { .with_children(|player| {
player.spawn(( player.spawn((
Name::new("PlayerSprite"),
SpriteBundle { SpriteBundle {
texture: assets.load("img/Player.png"), texture: assets.load("img/Player.png"),
..default() ..default()