remove bad tilemap. mouse experiment
This commit is contained in:
+32
-41
@@ -6,16 +6,11 @@ use rand::prelude::*;
|
|||||||
|
|
||||||
pub const PIXEL_RATIO: f32 = 4.0;
|
pub const PIXEL_RATIO: f32 = 4.0;
|
||||||
|
|
||||||
#[derive(Resource)]
|
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
pub struct TileMap {
|
setup_tilemap(&mut commands, &asset_server);
|
||||||
pub tiles: Vec<Vec<Vec<Entity>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>, mut tilemap: ResMut<TileMap>) {
|
for x in -50..50 {
|
||||||
setup_tilemap(&mut commands, &asset_server, tilemap);
|
for y in -25..25 {
|
||||||
|
|
||||||
for x in 0..25 {
|
|
||||||
for y in 0..250 {
|
|
||||||
if random::<f32>() < 0.1 {
|
if random::<f32>() < 0.1 {
|
||||||
commands.spawn(Citizen::new(
|
commands.spawn(Citizen::new(
|
||||||
&asset_server,
|
&asset_server,
|
||||||
@@ -26,40 +21,18 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>, mut t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>, mut tilemap: ResMut<TileMap>) {
|
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||||
for x in 0..25 {
|
for x in -50..50 {
|
||||||
let mut row:Vec<Vec<Entity>> = Vec::new();
|
for y in -25..25 {
|
||||||
for y in 0..25 {
|
|
||||||
let mut column:Vec<Entity> = Vec::new();
|
|
||||||
for z in 0..4 {
|
for z in 0..4 {
|
||||||
let position = Vec3::new(
|
let position = Vec3::new(
|
||||||
x as f32 * 16. * PIXEL_RATIO,
|
x as f32 * 16. * PIXEL_RATIO,
|
||||||
y as f32 * 16. * PIXEL_RATIO,
|
y as f32 * 16. * PIXEL_RATIO,
|
||||||
-z as f32,
|
-z as f32,
|
||||||
);
|
);
|
||||||
let mut noitem: bool = false;
|
|
||||||
|
|
||||||
let tile_entity: Entity;
|
|
||||||
if random::<f32>() < 0.4 {
|
|
||||||
tile_entity = commands
|
|
||||||
.spawn(TilePrefab::grass(position, asset_server))
|
|
||||||
.id();
|
|
||||||
} else if random::<f32>() < 0.2 {
|
|
||||||
tile_entity = commands
|
|
||||||
.spawn(TilePrefab::dirt(position, asset_server))
|
|
||||||
.id();
|
|
||||||
} else if random::<f32>() < 0.1 {
|
|
||||||
tile_entity = commands
|
|
||||||
.spawn(TilePrefab::rock(position, asset_server))
|
|
||||||
.id();
|
|
||||||
} else {
|
|
||||||
tile_entity = commands.spawn(TilePrefab::air(position, asset_server)).id();
|
|
||||||
noitem = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut items: Vec<ItemBundle> = vec![];
|
let mut items: Vec<ItemBundle> = vec![];
|
||||||
|
|
||||||
if !noitem {
|
|
||||||
if random::<f32>() < 0.1 {
|
if random::<f32>() < 0.1 {
|
||||||
items.push(ItemBundle {
|
items.push(ItemBundle {
|
||||||
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
|
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
|
||||||
@@ -93,17 +66,35 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>, mut t
|
|||||||
visibility: Visibility::Visible,
|
visibility: Visibility::Visible,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let mut child_entities = Vec::new();
|
if random::<f32>() < 0.7 {
|
||||||
|
commands
|
||||||
|
.spawn(TilePrefab::grass(position, asset_server))
|
||||||
|
.with_children(|parent| {
|
||||||
for item in items {
|
for item in items {
|
||||||
child_entities.push(commands.spawn(item).id());
|
parent.spawn(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if random::<f32>() < 0.2 {
|
||||||
|
commands
|
||||||
|
.spawn(TilePrefab::dirt(position, asset_server))
|
||||||
|
.with_children(|parent| {
|
||||||
|
for item in items {
|
||||||
|
parent.spawn(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if random::<f32>() < 0.1 {
|
||||||
|
commands
|
||||||
|
.spawn(TilePrefab::rock(position, asset_server))
|
||||||
|
.with_children(|parent| {
|
||||||
|
for item in items {
|
||||||
|
parent.spawn(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
commands.spawn(TilePrefab::air(position, asset_server));
|
||||||
}
|
}
|
||||||
commands.entity(tile_entity).add_children(&child_entities);
|
|
||||||
column.push(tile_entity);
|
|
||||||
}
|
}
|
||||||
row.push(column);
|
|
||||||
}
|
}
|
||||||
tilemap.tiles.push(row);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-8
@@ -1,12 +1,15 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
mod game;
|
|
||||||
mod citizen;
|
|
||||||
mod camera;
|
mod camera;
|
||||||
|
mod citizen;
|
||||||
|
mod game;
|
||||||
mod item;
|
mod item;
|
||||||
mod tile;
|
mod tile;
|
||||||
mod tiles;
|
mod tiles;
|
||||||
|
|
||||||
|
use bevy::color::palettes::basic::WHITE;
|
||||||
|
use game::PIXEL_RATIO;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(
|
.add_plugins(
|
||||||
@@ -21,14 +24,43 @@ fn main() {
|
|||||||
.set(ImagePlugin::default_nearest()),
|
.set(ImagePlugin::default_nearest()),
|
||||||
)
|
)
|
||||||
.insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.15)))
|
.insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.15)))
|
||||||
.add_systems(Startup, (
|
.add_systems(Startup, (game::setup_level, camera::spawn_panning_camera))
|
||||||
game::setup_level,
|
.add_systems(
|
||||||
camera::spawn_panning_camera,
|
FixedUpdate,
|
||||||
))
|
(
|
||||||
.add_systems(FixedUpdate, (
|
|
||||||
camera::camera_movement,
|
camera::camera_movement,
|
||||||
citizen::citizen_movement,
|
citizen::citizen_movement,
|
||||||
tile::tile_sprite_update,
|
tile::tile_sprite_update,
|
||||||
))
|
),
|
||||||
|
)
|
||||||
|
.add_systems(FixedUpdate, draw_cursor)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn draw_cursor(
|
||||||
|
camera_query: Single<(&Camera, &GlobalTransform)>,
|
||||||
|
windows: Query<&Window>,
|
||||||
|
mut gizmos: Gizmos,
|
||||||
|
) {
|
||||||
|
let (camera, camera_transform) = *camera_query;
|
||||||
|
|
||||||
|
let Ok(window) = windows.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(cursor_position) = window.cursor_position() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate a world position based on the cursor's position.
|
||||||
|
let Ok(point) = camera.viewport_to_world_2d(camera_transform, cursor_position) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"pos: {}, {}",
|
||||||
|
(point.x / (16. * PIXEL_RATIO)) as i32,
|
||||||
|
(point.y / (16. * PIXEL_RATIO)) as i32
|
||||||
|
);
|
||||||
|
|
||||||
|
gizmos.circle_2d(point, 10., WHITE);
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,14 +15,6 @@ impl Default for Tile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile {
|
|
||||||
pub fn new(position: Vec3) -> Self {
|
|
||||||
Self {
|
|
||||||
position,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Tile)]
|
#[require(Tile)]
|
||||||
pub struct FloorTile {
|
pub struct FloorTile {
|
||||||
|
|||||||
+1
-11
@@ -1,17 +1,7 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use crate::tile::{Tile, TileState};
|
||||||
use crate::game::PIXEL_RATIO;
|
use crate::game::PIXEL_RATIO;
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
#[require(Sprite)]
|
|
||||||
pub struct Tile {
|
|
||||||
pub position: Vec3,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct TileState {
|
|
||||||
pub timer: Timer,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Bundle)]
|
#[derive(Bundle)]
|
||||||
pub struct TilePrefab {
|
pub struct TilePrefab {
|
||||||
transform: Transform,
|
transform: Transform,
|
||||||
|
|||||||
Reference in New Issue
Block a user