Skip to content

Commit 67df940

Browse files
author
StarArawn
committed
Fully integrated bevy_ecs_tilemap and restored most of the existing func
1 parent a684cf5 commit 67df940

File tree

16 files changed

+233
-270
lines changed

16 files changed

+233
-270
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ edition = "2018"
99
[dependencies]
1010
bevy = "0.5"
1111
bevy_egui = "0.4"
12-
bevy_tilemap = { git="https://github.com/StarArawn/bevy_tilemap.git", branch="fix-precision" }
12+
bevy_ecs_tilemap = { path="../bevy_ecs_tilemap" } #{ git="https://github.com/StarArawn/bevy_ecs_tilemap", rev="13adc2e4405115a8243249cdae38d796cc70149b" }
1313
big-brain = "0.5"
1414
noise = "0.7"
1515
pathfinding = "2.1"
16-
rand = "0.8"
16+
fastrand = "1.3.4"

examples/visual_ai.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use bevy::{prelude::*};
22
use big_brain::prelude::*;
3-
use rand::{thread_rng, Rng};
43

5-
const HALF_WIDTH: f32 = (1270.0 / 2.0) - 32.0;
6-
const HALF_HEIGHT: f32 = (720.0 / 2.0) - 32.0;
4+
const HALF_WIDTH: i32 = (1270 / 2) - 32;
5+
const HALF_HEIGHT: i32 = (720 / 2) - 32;
76

87
struct Moving;
98

@@ -122,10 +121,9 @@ fn move_system(
122121
match *state {
123122
ActionState::Requested => {
124123
if let Ok(mut target) = target_query.get_mut(actor.0) {
125-
let mut random = thread_rng();
126124
let random_target = Vec2::new(
127-
random.gen_range(-HALF_WIDTH..HALF_WIDTH),
128-
random.gen_range(-HALF_HEIGHT..HALF_HEIGHT),
125+
fastrand::i32(-HALF_WIDTH..HALF_WIDTH) as f32,
126+
fastrand::i32(-HALF_HEIGHT..HALF_HEIGHT) as f32,
129127
);
130128
if target.value == Vec2::ZERO {
131129
target.value = random_target;
@@ -242,11 +240,9 @@ fn startup(
242240
let potion_texture = asset_server.load("textures/health_potion.png");
243241
let potion_material = materials.add(potion_texture.into());
244242

245-
let mut random = thread_rng();
246-
247243
for _ in 0..500 {
248-
let x = random.gen_range(-HALF_WIDTH..HALF_WIDTH);
249-
let y = random.gen_range(-HALF_HEIGHT..HALF_HEIGHT);
244+
let x = fastrand::i32(-HALF_WIDTH..HALF_WIDTH) as f32;
245+
let y = fastrand::i32(-HALF_HEIGHT..HALF_HEIGHT) as f32;
250246
commands.spawn_bundle(SpriteBundle {
251247
material: potion_material.clone(),
252248
transform: Transform::from_xyz(x, y, 0.0),

src/game/camera/input.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::game::GameState;
2-
use bevy::{
3-
prelude::*,
4-
render::camera::Camera,
5-
};
2+
use bevy::{prelude::*, render::camera::{Camera, OrthographicProjection}};
63

74
use super::CustomOrthographicProjection;
85

@@ -32,7 +29,7 @@ pub fn camera_movement(
3229
mut query: Query<(
3330
&mut Camera,
3431
&mut Transform,
35-
&mut CustomOrthographicProjection,
32+
&mut OrthographicProjection,
3633
)>,
3734
time: Res<Time>,
3835
_windows: Res<Windows>,
@@ -50,7 +47,7 @@ pub fn camera_movement(
5047

5148
for (mut _camera, mut transform, mut projection) in query.iter_mut() {
5249
let mut direction = Vec3::ZERO;
53-
let scale = projection.scale;
50+
let scale = transform.scale.x;
5451

5552
if keyboard_input.pressed(KeyCode::A) {
5653
direction -= Vec3::new(1.0, 0.0, 0.0);
@@ -70,16 +67,14 @@ pub fn camera_movement(
7067

7168
if keyboard_input.pressed(KeyCode::Z) && scale < 6.0 {
7269
let scale = ((scale + (time.delta_seconds() * 1.5)) * 100.0).round() / 100.0;
73-
projection.scale = scale;
70+
transform.scale = Vec3::splat(scale);
7471
}
7572

7673
if keyboard_input.pressed(KeyCode::X) && scale > 0.5 {
7774
let scale = ((scale - (time.delta_seconds() * 1.5)) * 100.0).round() / 100.0;
78-
projection.scale = scale;
75+
transform.scale = Vec3::splat(scale);
7976
}
8077

81-
82-
8378
transform.translation += time.delta_seconds() * direction * 1000.;
8479
}
8580
}

src/game/game_state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
22
pub enum GameState {
33
Loading,
4-
Generating,
4+
SpawnMap,
5+
GenerateMap,
6+
GenerateRoads,
57
MapView,
68
BattleView,
79
}

src/game/gameplay/attributes/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashMap;
21
use bevy::prelude::*;
32

43
#[derive(Debug, Clone, Copy)]

src/game/gameplay/camera/movement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy::{prelude::*, render::camera::{Camera, CameraProjection}};
1+
use bevy::{prelude::*, render::camera::{Camera, CameraProjection, OrthographicProjection}};
22

33
use crate::game::{camera::{CameraData, CustomOrthographicProjection}, gameplay::character::PlayerSprite};
44

@@ -10,7 +10,7 @@ pub fn movement(
1010
&mut CameraData,
1111
&mut Camera,
1212
&mut Transform,
13-
&mut CustomOrthographicProjection,
13+
&mut OrthographicProjection,
1414
)>,
1515
) {
1616
let mut player_position = Vec3::ZERO;
@@ -20,7 +20,7 @@ pub fn movement(
2020
for (mut camera_data, mut camera, mut camera_transform, mut projection) in camera_query.iter_mut() {
2121
let camera_z = camera_transform.translation.z;
2222

23-
camera_transform.translation = camera_transform.translation.truncate().lerp(player_position.truncate(), 0.01).extend(camera_z);
23+
// camera_transform.translation = camera_transform.translation.truncate().lerp(player_position.truncate(), 0.01).extend(camera_z);
2424

2525
projection.update(
2626
windows.get_primary().unwrap().width(),

src/game/gameplay/character/movement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::game::map::Map;
1+
use crate::game::map::MapData;
22
use bevy::prelude::*;
33

44
use super::PlayerSprite;
55

66
pub fn movement(
77
time: Res<Time>,
8-
map: Res<Map>,
8+
map: Res<MapData>,
99
mut player_query: Query<(&mut PlayerSprite, &mut Transform)>,
1010
) {
1111
if map.road_path.len() > 0 {

src/game/gameplay/enemy/spawner.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use bevy::prelude::*;
2-
use rand::{thread_rng, Rng};
3-
42
use crate::game::GameState;
53
use super::spawn_enemy;
64

@@ -55,12 +53,10 @@ pub fn tick(
5553
Vec2::new(0.0, -16.0),
5654
];
5755

58-
let mut random = thread_rng();
59-
6056
for (transform, mut spawner) in spawner_query.iter_mut() {
6157
let spawner_elapsed_time = current_time - spawner.last_time;
6258
if spawner_elapsed_time > spawner.wait_time && spawner.current < spawner.limit {
63-
let offset = spawn_pos[random.gen_range(0..4)];
59+
let offset = spawn_pos[fastrand::usize(0..4)];
6460
spawn_enemy(
6561
&mut commands,
6662
&asset_server,

src/game/gameplay/scenes/map_view.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
use bevy::{prelude::*, render::camera::RenderLayers};
2-
use bevy_tilemap::prelude::*;
2+
use bevy_ecs_tilemap::prelude::*;
33

4-
use crate::game::{camera::{CameraData, CurrentCamera, CustomOrthographicCameraBundle}};
4+
use crate::game::{camera::{CameraData, CurrentCamera}};
55

66
pub fn spawn(
77
mut commands: Commands,
88
mut current_camera: ResMut<CurrentCamera>,
9-
mut tilemap_query: Query<&mut Tilemap>,
9+
mut tilemap_query: Query<&mut Map>,
1010
) {
11-
let mut ortho = CustomOrthographicCameraBundle::new_2d();
11+
let mut ortho = OrthographicCameraBundle::new_2d();
1212
ortho.orthographic_projection.scale = 0.5;
1313

1414
let camera_entity = commands
1515
.spawn()
1616
.insert_bundle(ortho)
1717
.insert(CameraData::default())
18-
.insert(RenderLayers::layer(0))
18+
// .insert(RenderLayers::layer(0))
1919
.id();
2020

2121
current_camera.camera = Some(camera_entity);
2222

2323
for mut tilemap in tilemap_query.iter_mut() {
24-
let map_width = tilemap.width().unwrap() as i32;
25-
let map_height = tilemap.height().unwrap() as i32;
26-
let half_map_width = map_width / 2;
27-
let half_map_height = map_height / 2;
28-
29-
for x in -half_map_width..half_map_width {
30-
for y in -half_map_height..half_map_height {
31-
tilemap.spawn_chunk((x, y)).unwrap();
32-
}
33-
}
24+
// let map_width = tilemap.width().unwrap() as i32;
25+
// let map_height = tilemap.height().unwrap() as i32;
26+
// let half_map_width = map_width / 2;
27+
// let half_map_height = map_height / 2;
28+
29+
// for x in -half_map_width..half_map_width {
30+
// for y in -half_map_height..half_map_height {
31+
// tilemap.spawn_chunk((x, y)).unwrap();
32+
// }
33+
// }
3434
}
3535
}
3636

3737
pub fn destroy(
3838
mut commands: Commands,
3939
mut current_camera: ResMut<CurrentCamera>,
40-
mut tilemap_query: Query<&mut Tilemap>,
40+
mut tilemap_query: Query<&mut Map>,
4141
) {
4242
commands.entity(current_camera.camera.take().unwrap()).despawn_recursive();
4343

4444
for mut tilemap in tilemap_query.iter_mut() {
45-
let map_width = tilemap.width().unwrap() as i32;
46-
let map_height = tilemap.height().unwrap() as i32;
47-
let half_map_width = map_width / 2;
48-
let half_map_height = map_height / 2;
49-
50-
for x in -half_map_width..half_map_width {
51-
for y in -half_map_height..half_map_height {
52-
tilemap.despawn_chunk((x, y)).unwrap();
53-
}
54-
}
45+
// let map_width = tilemap.width().unwrap() as i32;
46+
// let map_height = tilemap.height().unwrap() as i32;
47+
// let half_map_width = map_width / 2;
48+
// let half_map_height = map_height / 2;
49+
50+
// for x in -half_map_width..half_map_width {
51+
// for y in -half_map_height..half_map_height {
52+
// tilemap.despawn_chunk((x, y)).unwrap();
53+
// }
54+
// }
5555
}
5656
}

src/game/loading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ pub fn loading(
1414
if asset_server.get_group_load_state(textures.iter().map(|(handle_id, _)| handle_id)) == LoadState::Loaded &&
1515
get_has_map_assets(asset_server, tilemap_atlas_handles)
1616
{
17-
game_state.set(GameState::Generating).unwrap();
17+
game_state.set(GameState::SpawnMap).unwrap();
1818
}
1919
}

0 commit comments

Comments
 (0)