Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ bones_lib = { git = "https://github.com/fishfolk/bones" }
bones_bevy_asset = { git = "https://github.com/fishfolk/bones" }
type_ulid = { git = "https://github.com/fishfolk/bones" }
bones_bevy_renderer = { git = "https://github.com/fishfolk/bones" }

# bones_lib = { path = "../bones/" }
# bones_bevy_asset = { path = "../bones/crates/bones_bevy_asset" }
# type_ulid = { path = "../bones/crates/type_ulid" }
# bones_bevy_renderer = { path = "../bones/crates/bones_bevy_renderer" }

3 changes: 3 additions & 0 deletions assets/locales/en-US/editor.ftl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
layers = Layers
create-layer = Create a New Layer
map-editor-preview-warning = ⚠ Warning: the map editor is a work-in-progress preview and may be broken or crash.

map-editor = Map Editor
map-info = Map Info
map-export = Map Export
tiles = Tiles
Expand All @@ -17,6 +19,7 @@ layer-kind = Layer Kind
create-map = Create Map
name = Name
grid-size = Grid Size
copy-to-clipboard = Copy to Clipboard

tilemap-path = Tilemap Path

Expand Down
33 changes: 12 additions & 21 deletions core/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
use crate::prelude::*;

pub fn install(session: &mut GameSession) {
// Spawn the camera and parallax background.
session
.world
.run_system(
|mut entities: ResMut<Entities>,
mut cameras: CompMut<Camera>,
mut transforms: CompMut<Transform>,
mut camera_shakes: CompMut<CameraShake>,
mut camera_follows: CompMut<CameraState>| {
let ent = entities.create();

camera_shakes.insert(ent, CameraShake::new(6.0, glam::vec2(3.0, 3.0), 1.0));
cameras.insert(ent, default());
transforms.insert(ent, default());
camera_follows.insert(ent, default());
},
)
.unwrap();

session
.stages
.add_system_to_stage(CoreStage::Last, camera_controller);
Expand All @@ -39,6 +20,9 @@ pub struct ParallaxBackgroundSprite {
#[ulid = "01GPV6M1KY0GBRQVJ3WG5CSBBS"]
pub struct CameraState {
pub player_camera_rects: [Rect; MAX_PLAYERS],
/// Disables the default camera controller. Useful, for example, when taking over the camera
/// from the editor.
pub disable_controller: bool,
}

fn camera_controller(
Expand All @@ -62,6 +46,9 @@ fn camera_controller(
let Some((_ent, (camera, camera_shake, camera_state))) = entities.iter_with((&mut cameras, &mut camera_shakes, &mut camera_states)).next() else {
return
};
if camera_state.disable_controller {
return;
}

// Update player camera rects
for (_ent, (transform, player_idx, body)) in
Expand Down Expand Up @@ -105,10 +92,14 @@ fn camera_controller(
}
}

let window_aspect = window.size.x / window.size.y;
let viewport_size = camera
.viewport
.map(|x| x.size.as_vec2())
.unwrap_or(window.size);
let viewport_aspect = viewport_size.x / viewport_size.y;
let default_height = meta.default_height;
let mut scale = camera.height / default_height;
let default_width = window_aspect * default_height;
let default_width = viewport_aspect * default_height;
let map_size = map.grid_size.as_vec2() * map.tile_size;

let mut min = Vec2::new(f32::MAX, f32::MAX);
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub fn install_modules(session: &mut session::GameSession) {
map::install(session);
elements::install(session);
player::install(session);
camera::install(session);
damage::install(session);
camera::install(session);
lifetime::install(session);
random::install(session);
debug::install(session);
Expand Down
19 changes: 19 additions & 0 deletions core/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,32 @@ fn spawn_map(
mut tile_collisions: CompMut<TileCollisionKind>,
mut parallax_bg_sprites: CompMut<ParallaxBackgroundSprite>,
mut sprites: CompMut<Sprite>,
mut cameras: CompMut<Camera>,
mut camera_shakes: CompMut<CameraShake>,
mut camera_states: CompMut<CameraState>,
) {
if map_spawned.0 {
return;
}
let Some(map) = map_assets.get(&map_handle.get_bevy_handle()) else {
return;
};

// Spawn the camera
{
let ent = entities.create();
camera_shakes.insert(
ent,
CameraShake {
center: (map.tile_size * (map.grid_size / 2).as_vec2()).extend(0.0),
..CameraShake::new(6.0, glam::vec2(3.0, 3.0), 1.0)
},
);
cameras.insert(ent, default());
transforms.insert(ent, default());
camera_states.insert(ent, default());
}

map_spawned.0 = true;
**clear_color = map.background_color.0;

Expand Down
15 changes: 15 additions & 0 deletions core/src/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ impl<'de> Deserialize<'de> for ColorMeta {
}
}

impl Serialize for ColorMeta {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let [r, g, b, a] = [
(self.0[0] * 255.0) as u8,
(self.0[1] * 255.0) as u8,
(self.0[2] * 255.0) as u8,
(self.0[3] * 255.0) as u8,
];
serializer.serialize_str(&format!("rgba({r}, {g}, {b}, {a})"))
}
}

struct ColorVisitor;
impl<'de> serde::de::Visitor<'de> for ColorVisitor {
type Value = ColorMeta;
Expand Down
18 changes: 9 additions & 9 deletions core/src/metadata/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
#[derive(BonesBevyAsset, Deserialize, Clone, TypeUlid, Debug)]
#[derive(BonesBevyAsset, Serialize, Deserialize, Clone, TypeUlid, Debug)]
#[ulid = "01GP264BT87MAAHMEK52Y5P7BW"]
#[asset_id = "map"]
#[serde(deny_unknown_fields)]
Expand All @@ -18,14 +18,14 @@ pub struct MapMeta {
pub layers: Vec<MapLayerMeta>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug, Default)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct BackgroundMeta {
pub speed: Vec2,
pub layers: Vec<ParallaxLayerMeta>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug, TypeUlid)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug, TypeUlid)]
#[serde(deny_unknown_fields)]
#[ulid = "01GPP1QJFVQN3HYW4N7ZE3S89Y"]
pub struct ParallaxLayerMeta {
Expand All @@ -37,7 +37,7 @@ pub struct ParallaxLayerMeta {
pub offset: Vec2,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct MapLayerMeta {
pub id: String,
Expand All @@ -47,36 +47,36 @@ pub struct MapLayerMeta {
pub entity: Option<Entity>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
pub enum MapLayerKind {
Tile(MapTileLayer),
Element(ElementLayer),
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug, Default)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct MapTileLayer {
pub tilemap: Handle<Atlas>,
pub has_collision: bool,
pub tiles: Vec<MapTileMeta>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug, Default)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct ElementLayer {
pub elements: Vec<ElementSpawn>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Clone, Debug, Default)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct ElementSpawn {
pub pos: Vec2,
pub element: Handle<ElementMeta>,
}

#[derive(BonesBevyAssetLoad, Deserialize, Serialize, Clone, Debug)]
#[derive(BonesBevyAssetLoad, Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct MapTileMeta {
pub pos: UVec2,
Expand Down
14 changes: 14 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ impl<'w, 's> GameLoader<'w, 's> {
let mut visuals = egui::Visuals::dark();
visuals.widgets = game.ui_theme.widgets.get_egui_widget_style();
visuals.window_fill = game.ui_theme.debug_window_fill.into_egui();
visuals.panel_fill = visuals.window_fill;
let [red, green, blue, alpha] = visuals.window_fill.to_srgba_unmultiplied();
let [red, green, blue, alpha] = [
red as f32 / 255.0,
green as f32 / 255.0,
blue as f32 / 255.0,
alpha as f32 / 255.0,
];
commands.insert_resource(ClearColor(Color::Rgba {
red,
green,
blue,
alpha,
}));
egui_ctx.ctx_mut().set_visuals(visuals);

// Helper to load border images
Expand Down
Loading