Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add anchor item implemented with the new Lua scripting system. #870

Merged
merged 2 commits into from
Nov 25, 2023
Merged
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
674 changes: 436 additions & 238 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ peg = "0.8.1"
egui_extras = { version = "0.23.0", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
directories = "1.0"
bevy_dylib = "0.11"

# anyhow = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion assets/game.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ music:
results_screen: music/11 thar she blows!.ogg
credits: music/12 all hands hoay!.ogg


main_menu:
title_font:
family: Fairfax SM
Expand Down Expand Up @@ -297,6 +296,7 @@ core:
- /map/resources/coral.atlas.yaml

map_elements:
- /plugins/anchor/element.yaml
- /elements/decoration/anemones/anemones.element.yaml
- /elements/decoration/seaweed/seaweed.element.yaml
- /elements/environment/urchin/urchin.element.yaml
Expand Down
4 changes: 4 additions & 0 deletions assets/map/levels/level_1.map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,10 @@ layers:
- 432.0
- 555.0
element: /elements/item/sword/sword.element.yaml
- pos:
- 300.0
- 700.0
element: /plugins/anchor/element.yaml
- pos:
- 480.0
- 118.5
Expand Down
4 changes: 4 additions & 0 deletions assets/pack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
root: game.yaml
schemas:
- plugins/anchor/AnchorMeta.schema.yaml
- plugins/anchor/IdleAnchor.schema.yaml
- plugins/anchor/FallingAnchor.schema.yaml
17 changes: 17 additions & 0 deletions assets/plugins/anchor/AnchorMeta.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: AnchorMeta
full_name: core::AnchorMeta
asset_extension: anchor
kind: !Struct
fields:
- name: atlas
# Handle<Atlas>
schema: UntypedHandle
- name: fall_speed
schema: f32
- name: body_size
schema: Vec2
- name: fin_anim
schema: Ustr
- name: grab_offset
schema: Vec2

3 changes: 3 additions & 0 deletions assets/plugins/anchor/FallingAnchor.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: FallingAnchor
full_name: core::FallingAnchor
kind: !Struct
3 changes: 3 additions & 0 deletions assets/plugins/anchor/IdleAnchor.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: IdleAnchor
full_name: core::IdleAnchor
kind: !Struct
5 changes: 5 additions & 0 deletions assets/plugins/anchor/anchor.atlas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
image: ./anchor.png
tile_size: [28, 35]
columns: 1
rows: 1

Binary file added assets/plugins/anchor/anchor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions assets/plugins/anchor/anchor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
atlas: ./anchor.atlas.yaml
grab_offset: [5, -2]
body_size: [28, 34]
fin_anim: grab_2
fall_speed: 8
4 changes: 4 additions & 0 deletions assets/plugins/anchor/element.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: Anchor
category: Weapons
data: anchor.yaml
plugin: plugin.lua
95 changes: 95 additions & 0 deletions assets/plugins/anchor/plugin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
local Entities = s"Entities"
local MapElementHydrated = s"MapElementHydrated"
local ElementHandle = s"ElementHandle"
local AnchorMeta = s"AnchorMeta"
local AtlasSprite = s"AtlasSprite"
local Item = s"Item"
local ItemThrow = s"ItemThrow"
local ItemGrab = s"ItemGrab"
local DehydrateOutOfBounds = s"DehydrateOutOfBounds"
local KinematicBody = s"KinematicBody"
local Transform = s"Transform"
local DropItem = s"DropItem"
local ItemUsed = s"ItemUsed"
local IdleAnchor = s"IdleAnchor"
local FallingAnchor = s"FallingAnchor"
local DamageRegion = s"DamageRegion"
local DamageRegionOwner = s"DamageRegionOwner"

local function hydrate()
local entities = resources:get(Entities)

for spawner_ent, element_handle in entities:iter_with(ElementHandle, MapElementHydrated:without()) do
local element = assets:get(element_handle[0])
local anchor_meta = assets:get(element.data)

if schema_of(anchor_meta) == AnchorMeta then
-- Spawn an anchor
local ent = entities:create()
local sprite = AtlasSprite:create();
sprite.atlas = anchor_meta.atlas
components:insert(ent, element_handle)
components:insert(ent, MapElementHydrated:create())
components:insert(ent, IdleAnchor:create())
components:insert(ent, sprite)
components:insert(ent, components:get(spawner_ent, Transform))
components:insert(ent, Item:create())
local item_grab = ItemGrab:create()
item_grab.fin_anim = anchor_meta.fin_anim
item_grab.grab_offset = anchor_meta.grab_offset
components:insert(ent, item_grab)
components:insert(ent, ItemThrow:create())
local dehydrate_out_of_bounds = DehydrateOutOfBounds:create()
dehydrate_out_of_bounds[0] = spawner_ent
components:insert(ent, dehydrate_out_of_bounds)
local body = KinematicBody:create()
-- TODO: Set the body shape and size. Doesn't work in lua yet because
-- the body shape is an enum.
body.gravity = assets.root.core.physics.gravity
body.has_mass = true
body.has_friction = true
body.bounciness = 0
components:insert(ent, body)

-- Mark spawner as hydrated
components:insert(spawner_ent, MapElementHydrated:create())
end
end
end

local function update()
local entities = resources:get(Entities)

for ent in entities:iter_with(IdleAnchor) do
local element_handle = components:get(ent, ElementHandle)
local element = assets:get(element_handle[0])
local anchor_meta = assets:get(element.data)

local used = components:get(ent, ItemUsed)
if used then
-- components:remove(ent, ItemUsed)
components:remove(ent, KinematicBody)
components:remove(ent, IdleAnchor)
components:insert(ent, FallingAnchor:create())
components:insert(ent, DropItem:create())
local damage = DamageRegion:create()
damage.size = anchor_meta.body_size
components:insert(ent, damage)
local damageOwner = DamageRegionOwner:create()
damageOwner[0] = used.owner
components:insert(ent, damageOwner)
end
end

for ent in entities:iter_with(FallingAnchor) do
local element_handle = components:get(ent, ElementHandle)
local element = assets:get(element_handle[0])
local anchor_meta = assets:get(element.data)

local trans = components:get(ent, Transform)
trans.translation.y = trans.translation.y - anchor_meta.fall_speed
end
end

session:add_system_to_stage(CoreStage.PreUpdate, hydrate)
session:add_system_to_stage(CoreStage.PostUpdate, update)
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.73.0
1.74
11 changes: 7 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub mod prelude {
}

pub fn game_plugin(game: &mut Game) {
PlayerMeta::schema();
AudioSource::schema();
HatMeta::schema();
MapMeta::schema();
PlayerMeta::register_schema();
AudioSource::register_schema();
HatMeta::register_schema();
MapMeta::register_schema();
game.install_plugin(elements::game_plugin)
.install_plugin(bullet::game_plugin)
.init_shared_resource::<AssetServer>();
Expand All @@ -50,6 +50,8 @@ pub fn game_plugin(game: &mut Game) {
pub struct MatchPlugin {
pub map: MapMeta,
pub player_info: [PlayerInput; MAX_PLAYERS],
/// The lua plugins to enable for this match.
pub plugins: Arc<Vec<Handle<LuaPlugin>>>,
}

pub struct MatchPlayerInfo {
Expand All @@ -63,6 +65,7 @@ impl SessionPlugin for MatchPlugin {
fn install(self, session: &mut Session) {
session
.install_plugin(DefaultSessionPlugin)
.install_plugin(LuaPluginLoaderSessionPlugin(self.plugins))
.install_plugin(audio::session_plugin);

physics::install(session);
Expand Down
2 changes: 1 addition & 1 deletion src/core/bullet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::prelude::*;

pub fn game_plugin(game: &mut Game) {
BulletMeta::schema();
BulletMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
6 changes: 6 additions & 0 deletions src/core/damage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use super::utils::Rect;

/// Install this module.
pub fn install(session: &mut Session) {
DamageRegion::register_schema();
DamageRegionOwner::register_schema();

session
.stages
.add_system_to_stage(CoreStage::PostUpdate, kill_players_in_damage_region);
Expand All @@ -18,6 +21,7 @@ pub fn install(session: &mut Session) {
/// While this _might_ change in the future, damage regions will kill players immediately, so there
/// is no "damage" field.
#[derive(Debug, Clone, Default, HasSchema)]
#[repr(C)]
pub struct DamageRegion {
/// The size of the damage region in pixels
pub size: Vec2,
Expand All @@ -33,7 +37,9 @@ impl DamageRegion {
/// A component that may be added to a damage region entity to indicate the triggering entity.
///
/// If this entity is a player, it will not be harmed by the damage region.
// TODO: Make `DamageRegionOwner` a part of the `DamageRegion` component?
#[derive(Debug, Clone, HasSchema, Default)]
#[repr(C)]
pub struct DamageRegionOwner(pub Entity);

/// System that will eliminate players that are intersecting with a damage region.
Expand Down
3 changes: 2 additions & 1 deletion src/core/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ impl FromWorld for RapierDebugContext {
fn from_world(world: &mut World) -> Self {
let path_entity = world.resource_mut::<Entities>().create();

let mut transforms = world.components.get_mut::<Transform>().unwrap();
let transforms = world.components.get::<Transform>();
let mut transforms = transforms.borrow_mut();
transforms.insert(
path_entity,
Transform::from_translation(vec3(0.0, 0.0, -1.0)),
Expand Down
11 changes: 10 additions & 1 deletion src/core/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct ElementMeta {
pub category: Ustr,
pub data: Handle<SchemaBox>,
pub editor: ElementEditorMeta,
pub plugin: Handle<LuaPlugin>,
}

#[derive(HasSchema, Deserialize, Clone, Debug)]
Expand All @@ -65,6 +66,7 @@ impl Default for ElementEditorMeta {

/// Marker component added to map elements that have been hydrated.
#[derive(Clone, HasSchema, Default)]
#[repr(C)]
pub struct MapElementHydrated;

/// Component that contains the [`Entity`] to de-hydrate when the entity with this component is out
Expand All @@ -73,10 +75,12 @@ pub struct MapElementHydrated;
/// This is useful for map elements that spawn items: when the item falls off the map, it should
/// de-hydrate it's spawner, so that the spawner will re-spawn the item in it's default state.
#[derive(Clone, HasSchema, Default, Deref, DerefMut)]
#[repr(C)]
pub struct DehydrateOutOfBounds(pub Entity);

/// Component containing an element's metadata handle.
#[derive(Clone, Copy, HasSchema, Default, Deref, DerefMut)]
#[repr(C)]
pub struct ElementHandle(pub Handle<ElementMeta>);

#[derive(Clone, HasSchema)]
Expand Down Expand Up @@ -178,6 +182,7 @@ impl<'a> SpawnerManager<'a> {

self.spawners.insert(entity, spawner);
}

/// Stores the spawned elements as having come from the same group of spawners as the spawner_elements.
pub fn insert_spawned_entity_into_grouped_spawner<T: HasSchema>(
&mut self,
Expand Down Expand Up @@ -260,6 +265,10 @@ impl<'a> SpawnerManager<'a> {
macro_rules! install_plugins {
($($module:ident),* $(,)?) => {
pub fn session_plugin(session: &mut Session) {
ElementHandle::register_schema();
MapElementHydrated::register_schema();
DehydrateOutOfBounds::register_schema();

session
.stages
.add_system_to_stage(CoreStage::First, handle_out_of_bounds_items);
Expand All @@ -270,7 +279,7 @@ macro_rules! install_plugins {
}

pub fn game_plugin(game: &mut Game) {
ElementMeta::schema();
ElementMeta::register_schema();
game.init_shared_resource::<AssetServer>();

$(
Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn session_plugin(session: &mut Session) {
}

pub fn game_plugin(game: &mut Game) {
CrabMeta::schema();
CrabMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/crate_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct CrateMeta {
}

pub fn game_plugin(game: &mut Game) {
CrateMeta::schema();
CrateMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/decoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct AnimatedDecorationMeta {
}

pub fn game_plugin(game: &mut Game) {
AnimatedDecorationMeta::schema();
AnimatedDecorationMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/fish_school.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct FishSchoolMeta {
}

pub fn game_plugin(game: &mut Game) {
FishSchoolMeta::schema();
FishSchoolMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/grenade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GrenadeMeta {
}

pub fn game_plugin(game: &mut Game) {
GrenadeMeta::schema();
GrenadeMeta::register_schema();
game.init_shared_resource::<AssetServer>();
}

Expand Down
9 changes: 3 additions & 6 deletions src/core/elements/kick_bomb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ pub struct KickBombMeta {
pub arm_delay: Duration,
}

pub fn game_plugin(game: &mut Game) {
KickBombMeta::schema();
game.init_shared_resource::<AssetServer>();
pub fn game_plugin(_game: &mut Game) {
KickBombMeta::register_schema();
}

pub fn session_plugin(session: &mut Session) {
Expand Down Expand Up @@ -63,7 +62,7 @@ fn hydrate(
mut atlas_sprites: CompMut<AtlasSprite>,
assets: Res<AssetServer>,
mut hydrated: CompMut<MapElementHydrated>,
mut element_handles: CompMut<ElementHandle>,
element_handles: Comp<ElementHandle>,
mut animated_sprites: CompMut<AnimatedSprite>,
mut respawn_points: CompMut<DehydrateOutOfBounds>,
mut spawner_manager: SpawnerManager,
Expand Down Expand Up @@ -113,8 +112,6 @@ fn hydrate(
atlas_sprites.insert(entity, AtlasSprite::new(*atlas));
respawn_points.insert(entity, DehydrateOutOfBounds(spawner_ent));
transforms.insert(entity, transform);
element_handles.insert(entity, element_handle);
hydrated.insert(entity, MapElementHydrated);
animated_sprites.insert(entity, default());
bodies.insert(
entity,
Expand Down
Loading