This commit is contained in:
Arkitu 2024-12-24 18:59:27 +01:00
parent d1dbb78f07
commit be658ded6e
7 changed files with 1564 additions and 45 deletions

View File

@ -1,2 +1,6 @@
[target.wasm32-unknown-unknown]
runner = "wasm-server-runner"
runner = "wasm-server-runner"
# [target.aarch64-unknown-linux-gnu]
# linker = "clang"
# rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]

1470
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[lib]
crate_type=["cdylib"]
crate-type=["cdylib"]
[[bin]]
path = "src/lib.rs"
@ -15,9 +15,12 @@ package = "com.forestiles.arkitu"
build_targets = ["armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android", "x86_64-linux-android"]
[dependencies]
bevy = { version = "0.15", default-features = false, features = [
"bevy_color","bevy_core_pipeline","bevy_render","bevy_winit","bevy_window","multi_threaded","wayland","bevy_sprite"
]}
# bevy = { version = "0.15", default-features = false, features = [
# "bevy_color","bevy_core_pipeline","bevy_render","bevy_winit","bevy_window","multi_threaded","wayland","bevy_sprite",
# "bevy_picking","bevy_mesh_picking_backend","bevy_ui_picking_backend","bevy_sprite_picking_backend"
# ]}
bevy = { version = "0.15" }
bevy-inspector-egui = { version = "0.28" }
winit = { version = "0.30", features = ["rwh_05", "android-native-activity"] }
env_logger = "0.11"
log = "0.4"
@ -47,3 +50,11 @@ getrandom ={ version = "*", features = ["js"]}
[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.14"
# Enable a small amount of optimization in the dev profile.
[profile.dev]
opt-level = 1
# Enable a large amount of optimization in the dev profile for dependencies.
[profile.dev.package."*"]
opt-level = 3

View File

@ -1,11 +1,13 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy::{picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::HashMap, window::PrimaryWindow};
use crate::ui;
pub struct Plugin;
impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(Update, move_cam)
.init_resource::<MousePos>();
.init_resource::<Pointers>();
}
}
@ -18,23 +20,40 @@ fn setup(mut cmds: Commands, window: Query<&Window>) {
}
#[derive(Resource, Default)]
struct MousePos(Vec2);
struct Pointers(HashMap<PointerId, Vec2>);
fn move_cam(
mut cam: Query<&mut Transform, With<Camera2d>>,
map_ui_entity: Query<Entity, With<ui::MapUIComponent>>,
mouse_buttons: Res<ButtonInput<MouseButton>>,
touches: Res<Touches>,
window: Query<&Window, With<PrimaryWindow>>,
mut mouse_pos: ResMut<MousePos>
mut pointers: ResMut<Pointers>,
hover_map: Res<HoverMap>
) {
let window = window.single();
if let Some(cursor_pos) = window.cursor_position() {
if mouse_buttons.pressed(MouseButton::Left) {
let delta = cursor_pos - mouse_pos.0;
dbg!(delta);
let mut cam = cam.single_mut();
cam.translation.x -= delta.x*cam.scale.x;
cam.translation.y += delta.y*cam.scale.y;
let mut cam = cam.single_mut();
let map_ui_entity = map_ui_entity.single();
for (id, hit_map) in hover_map.iter() {
if let Some((pressed, new_pos, old_pos)) = match id {
PointerId::Mouse => window.cursor_position().map(|p| (mouse_buttons.pressed(MouseButton::Left), p, None)),
PointerId::Touch(i) => touches.get_pressed(*i).map(|t| (true, t.position(), Some(t.previous_position()))),
_ => None
} {
let old_pos = old_pos.unwrap_or(pointers.0.get(id).map(|p| *p).unwrap_or_default());
// dbg!(pressed, &new_pos, &old_pos);
if pressed {
// dbg!(hit_map);
for entity in hit_map.keys() {
dbg!(entity);
if *entity == map_ui_entity {
let delta = new_pos - old_pos;
cam.translation.x -= delta.x*cam.scale.x;
cam.translation.y += delta.y*cam.scale.y;
}
}
}
pointers.0.insert(*id, new_pos);
}
mouse_pos.0 = cursor_pos;
}
}

View File

@ -1,15 +1,17 @@
// mod graphics;
// mod state;
mod map;
mod camera;
pub mod map;
pub mod camera;
pub mod ui;
use std::{fmt::Debug, sync::Arc};
use log::debug;
// use state::State;
// use graphics::Graphics;
use winit::{application::ApplicationHandler, dpi::PhysicalSize, event::{Event, WindowEvent}, event_loop::EventLoop, window::{Window, WindowAttributes}};
// use winit::{application::ApplicationHandler, dpi::PhysicalSize, event::{Event, WindowEvent}, event_loop::EventLoop, window::{Window, WindowAttributes}};
use bevy::{prelude::*, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
pub fn dbg<V: Debug>(v: V) -> V {
debug!(target: "app", "{:?}", v);
@ -103,8 +105,10 @@ pub fn main() {
App::new()
.add_plugins((
DefaultPlugins,
WorldInspectorPlugin::default(),
camera::Plugin,
map::Plugin,
ui::Plugin,
FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin {
filter: Some(vec![FrameTimeDiagnosticsPlugin::FPS]),

View File

@ -12,7 +12,8 @@ impl bevy::prelude::Plugin for Plugin {
app.add_systems(Startup, setup)
.insert_resource(Time::<Fixed>::from_seconds(0.25)) // Time for a day
.add_systems(FixedUpdate, update_cells)
.insert_resource(ClearColor(Color::srgb(0., 0., 1.)));
.insert_resource(ClearColor(Color::srgb(0., 0., 1.)))
.insert_resource(Seed(0));
}
}
@ -22,13 +23,15 @@ pub const REAL_HEIGHT: f32 = 500.;
pub const REAL_WIDTH: f32 = 500.;
pub const CELL_AREA: f32 = REAL_HEIGHT * REAL_WIDTH / SIZE as f32;
pub const SIZE: usize = 10000;
pub const seed: u32 = 0;
#[derive(Resource)]
struct Seed(u32);
#[derive(Component)]
struct Voronoi (voronoice::Voronoi);
#[derive(Component)]
struct MapMarker;
pub struct MapMarker;
#[derive(Component)]
struct MapColors (Vec<[f32; 4]>);
@ -36,9 +39,10 @@ struct MapColors (Vec<[f32; 4]>);
fn setup(
mut cmds: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>
mut materials: ResMut<Assets<ColorMaterial>>,
seed: Res<Seed>
) {
let mut rng = rand::rngs::SmallRng::seed_from_u64(seed as u64);
let mut rng = rand::rngs::SmallRng::seed_from_u64(seed.0 as u64);
let mut sites = Vec::with_capacity(SIZE);
for _ in 0..SIZE {
sites.push(Point { x:rng.gen_range(-WIDTH/2.0..WIDTH/2.0) as f64, y:rng.gen_range(-HEIGHT/2.0..HEIGHT/2.0) as f64 })
@ -50,8 +54,8 @@ fn setup(
.build()
.unwrap();
let mut cells_data = Vec::with_capacity(SIZE);
let z_noise = Fbm::<Perlin>::new(seed);
let moisture_noise = Fbm::<Perlin>::new(seed+1)
let z_noise = Fbm::<Perlin>::new(seed.0);
let moisture_noise = Fbm::<Perlin>::new(seed.0+1)
.set_frequency(2.);
for i in 0..SIZE {
let c = voronoi.cell(i);
@ -81,7 +85,7 @@ fn setup(
let mut colors = Vec::new();
let mut indices = Vec::new();
for (c, mut cd) in voronoi.iter_cells().zip(cells_data).filter(|(_,cd)| cd.kind != CellKind::Forest) {
for (c, mut cd) in voronoi.iter_cells().zip(cells_data.iter_mut()).filter(|(_,cd)| cd.kind != CellKind::Forest) {
let mut color = cd.color();
// if c.site() == selected_tile {
// color[0] = (color[0]+0.4).clamp(0., 1.);
@ -99,11 +103,6 @@ fn setup(
indices.extend_from_slice(&[i as u32, (i+v) as u32, (i+v+1) as u32]);
cd.vertices.extend_from_slice(&[i, i+v, i+v+1]);
}
match cd.kind {
CellKind::Forest | CellKind::Grass => cmds.spawn((cd, LastUpdate(0))),
_ => cmds.spawn(cd)
};
}
let mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default())
@ -126,7 +125,14 @@ fn setup(
Voronoi(voronoi),
MapColors(colors),
MapMarker
));
)).with_children(|parent| {
for cd in cells_data {
match cd.kind {
CellKind::Forest | CellKind::Grass => parent.spawn((cd, LastUpdate(0))),
_ => parent.spawn(cd)
};
}
});
}
#[derive(Component)]

27
src/ui.rs Normal file
View File

@ -0,0 +1,27 @@
use bevy::prelude::*;
pub struct Plugin;
impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
}
}
#[derive(Component)]
pub struct MapUIComponent;
fn setup(
mut cmds: Commands
) {
cmds.spawn((
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
MapUIComponent
));
// Spawn all ui elements as children of this one
}