Initial tile push with item cycle

This commit is contained in:
StephenAdamson
2024-12-12 17:19:59 +00:00
parent 5a56da91bb
commit 7bab01ee74
15 changed files with 5163 additions and 0 deletions
+1
View File
@@ -12,3 +12,4 @@
# Built Visual Studio Code Extensions
*.vsix
/target
Generated
+4725
View File
File diff suppressed because it is too large Load Diff
+23
View File
@@ -0,0 +1,23 @@
[package]
name = "dorf"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.15.0"
rand = "0.8.5"
# Enable max optimizations for dependencies, but not for our code:
[profile.dev.package."*"]
opt-level = 3
# Enable only a small amount of optimization in debug mode
[profile.dev]
opt-level = 1
[profile.release]
lto = true
opt-level = 3
codegen-units = 1
incremental = false
debug = false
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 851 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 752 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 709 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 689 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

+44
View File
@@ -0,0 +1,44 @@
use bevy::prelude::*;
use bevy::input::keyboard::KeyCode;
#[derive(Component)]
pub struct PanningCamera {
pub pan_speed: f32,
}
pub fn camera_movement(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&PanningCamera, &mut Transform), With<Camera>>,
) {
for (camera, mut transform) in query.iter_mut() {
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::ArrowUp) {
direction.y += 1.0;
}
if keyboard_input.pressed(KeyCode::ArrowDown) {
direction.y -= 1.0;
}
if keyboard_input.pressed(KeyCode::ArrowLeft) {
direction.x -= 1.0;
}
if keyboard_input.pressed(KeyCode::ArrowRight) {
direction.x += 1.0;
}
if direction.length() > 0.0 {
direction = direction.normalize();
let movement = direction * camera.pan_speed;
transform.translation.x += movement.x;
transform.translation.y += movement.y;
}
}
}
pub fn spawn_panning_camera(mut commands: Commands) {
commands.spawn((
Camera2d::default(),
PanningCamera { pan_speed: 5.0 }
));
}
+60
View File
@@ -0,0 +1,60 @@
use crate::game::PIXEL_RATIO;
use bevy::prelude::*;
use rand::prelude::*;
#[derive(Component)]
#[require(Transform)]
pub struct Ambulatory {
speed: f32,
}
#[derive(Bundle)]
pub struct Citizen {
walker: Ambulatory,
sprite: Sprite,
transform: Transform,
}
impl Citizen {
pub fn new(asset_server: &Res<AssetServer>, position: Vec3) -> Self {
Citizen {
walker: Ambulatory { speed: 64. },
sprite: Sprite {
image: asset_server.load("character.png"),
..Default::default()
},
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
}
}
}
pub fn citizen_movement(
mut query: Query<(&Ambulatory, &mut Transform), With<Ambulatory>>,
) {
for (citizen, mut transform) in query.iter_mut() {
if random::<f32>() < 0.66666{
continue;
}
let mut direction = Vec2::ZERO;
direction.y += rand::random::<f32>();
direction.y -= rand::random::<f32>();
direction.x -= rand::random::<f32>();
direction.x += rand::random::<f32>();
if direction.length() > 0.0 {
direction.x = direction.x.round();
direction.y = direction.y.round();
let movement = direction * citizen.speed;
transform.translation.x += movement.x;
transform.translation.y += movement.y;
if direction.x > 0.0 {
transform.scale.x = PIXEL_RATIO.abs();
} else if direction.x < 0.0 {
transform.scale.x = -PIXEL_RATIO.abs();
}
}
}
}
+99
View File
@@ -0,0 +1,99 @@
use crate::citizen::Citizen;
use crate::item::{Item, ItemBundle};
use crate::tile::{FloorTile, Tile, TileState};
use bevy::prelude::*;
use rand::prelude::*;
pub const PIXEL_RATIO: f32 = 4.0;
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
setup_tilemap(&mut commands, &asset_server);
for x in 0..25 {
for y in 0..25 {
if random::<f32>() < 0.1 {
commands.spawn(Citizen::new(
&asset_server,
Vec3::new(x as f32 * 64., y as f32 * 64., 1.),
));
}
}
}
}
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
for x in 0..25 {
for y in 0..25 {
let position = Vec3::new(
x as f32 * 16. * PIXEL_RATIO,
y as f32 * 16. * PIXEL_RATIO,
-0.5,
);
let texture = if random::<f32>() < 0.5 {
asset_server.load("grass.png")
} else {
asset_server.load("rock.png")
};
let mut items: Vec<ItemBundle> = vec![];
if random::<f32>() < 0.1 {
items.push(ItemBundle {
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
sprite: Sprite {
image: asset_server.load("beer.png"),
..Default::default()
},
item: Item::new(),
visibility: Visibility::Visible,
});
}
if random::<f32>() < 0.1 {
items.push(ItemBundle {
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
sprite: Sprite {
image: asset_server.load("chalice.png"),
..Default::default()
},
item: Item::new(),
visibility: Visibility::Visible,
});
}
if random::<f32>() < 0.1 {
items.push(ItemBundle {
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
sprite: Sprite {
image: asset_server.load("fish.png"),
..Default::default()
},
item: Item::new(),
visibility: Visibility::Visible,
});
}
let tile_entity = commands
.spawn((
Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
Sprite {
image: texture,
..Default::default()
},
FloorTile {
walkable: random::<f32>() < 0.7,
astar_weight: random::<u8>() % 10 + 1,
},
Tile { position: position },
TileState { timer: Timer::from_seconds(0.5, TimerMode::Repeating) }
))
.id();
let mut child_entities = Vec::new();
for item in items {
child_entities.push(commands.spawn(item).id());
}
commands.entity(tile_entity).add_children(&child_entities);
}
}
}
+47
View File
@@ -0,0 +1,47 @@
use bevy::prelude::*;
#[derive(Component)]
#[require(Sprite,Transform,Visibility )]
pub struct Item {
wear: u8,
quality: u8,
weight: u8,
stain_time: u16
}
impl Default for Item {
fn default() -> Self {
Self {
wear: 0,
quality: 0,
weight: 0,
stain_time: 16,
}
}
}
impl Item {
pub fn new() -> Self {
Self {
..Default::default()
}
}
}
#[derive(Bundle)]
pub struct ItemBundle {
pub item: Item,
pub transform: Transform,
pub sprite: Sprite,
pub visibility: Visibility
}
#[derive(Component)]
pub struct Nameable {
pub name: String
}
#[derive(Component)]
pub struct Compostable {
decay: u8,
}
+33
View File
@@ -0,0 +1,33 @@
use bevy::prelude::*;
mod game;
mod citizen;
mod camera;
mod item;
mod tile;
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Dorf"),
..Default::default()
}),
..Default::default()
})
.set(ImagePlugin::default_nearest()),
)
.insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.15)))
.add_systems(Startup, (
game::setup_level,
camera::spawn_panning_camera,
))
.add_systems(FixedUpdate, (
camera::camera_movement,
citizen::citizen_movement,
tile::tile_update,
))
.run();
}
+131
View File
@@ -0,0 +1,131 @@
use crate::item::Item;
use bevy::prelude::*;
#[derive(Component)]
#[require(Sprite)]
pub struct Tile {
pub position: Vec3,
}
impl Default for Tile {
fn default() -> Self {
Self {
position: Vec3::ZERO,
}
}
}
impl Tile {
pub fn new(position: Vec3) -> Self {
Self {
position,
}
}
}
#[derive(Component)]
#[require(Tile)]
pub struct FloorTile {
pub walkable: bool,
pub astar_weight: u8,
}
impl Default for FloorTile {
fn default() -> Self {
Self {
walkable: true,
astar_weight: 1,
}
}
}
#[derive(Component)]
pub struct WallTile {
pub tile: Tile,
pub solid: bool,
pub embrasure: bool, // Arrowslit, crenelle, grate, cage etc
}
#[derive(Component)]
pub struct WaterTile {
pub tile: Tile,
pub swimmable: bool,
pub astarmultiplier: f32,
}
#[derive(Component)]
pub struct DoorTile {
pub tile: Tile,
pub open: bool,
pub locked: bool,
}
pub fn spawn_tile(
commands: &mut Commands,
position: Vec3,
floor_tile: FloorTile,
// fixture: Option<Fixture>,
items: Vec<Item>,
) {
let tile_entity = commands.spawn((Tile { position }, floor_tile)).id();
// if let Some(fixture) = fixture {
// commands.entity(tile_entity).add_children(&[
// commands
// .spawn((fixture,))
// .id(),
// ]);
// }
let mut child_entities = Vec::new();
for item in items {
child_entities.push(commands.spawn(item).id());
}
commands.entity(tile_entity).add_children(&child_entities);
}
pub fn tile_update(
time: Res<Time>,
mut query_tile: Query<(Entity, &Children, &mut TileState), With<Tile>>,
mut query_item: Query<(&mut Visibility, &Item)>,
) {
for (tile_entity, children, mut state) in query_tile.iter_mut() {
state.timer.tick(time.delta());
if !state.timer.finished() {
continue;
}
let mut visible_index: Option<usize> = None;
let mut visible_child: Option<Entity> = None;
for (i, &child) in children.iter().enumerate() {
if let Ok((mut visibility, _)) = query_item.get_mut(child) {
if matches!(*visibility, Visibility::Visible) {
visible_index = Some(i);
visible_child = Some(child);
*visibility = Visibility::Hidden;
break;
}
}
}
let next_index = if let Some(current_index) = visible_index {
(current_index + 1) % children.len()
} else {
0
};
if let Some(&next_child) = children.get(next_index) {
if let Ok((mut visibility, _)) = query_item.get_mut(next_child) {
*visibility = Visibility::Visible;
// println!("{}",next_index.to_string());
}
}
}
}
#[derive(Component)]
pub struct TileState {
pub timer: Timer,
}