Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Refactor Godot .tscn spawner and top down scenes' actions (#233)
Browse files Browse the repository at this point in the history
* Refactored Godot .tscn spawner into an inheritance system.

* Removed local actions from top down scenes in favor of a single enum global to all top down scenes.
  • Loading branch information
porkbrain authored Sep 3, 2024
1 parent 0e03630 commit 614823b
Show file tree
Hide file tree
Showing 28 changed files with 807 additions and 907 deletions.
2 changes: 1 addition & 1 deletion main_game/assets/scenes/barn.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ position = Vector2(-124, 111)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"
2 changes: 1 addition & 1 deletion main_game/assets/scenes/clinic.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ position = Vector2(-123, 109)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/clinic_ward.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ position = Vector2(-85, 111)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/compound_tower.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ position = Vector2(-5, 103)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/mall.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ position = Vector2(239, 188)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/plant_shop.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ position = Vector2(-109, 99)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/sewers.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ position = Vector2(292, 152)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
2 changes: 1 addition & 1 deletion main_game/assets/scenes/twinpeaks_apartment.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ position = Vector2(-112, 88)

[node name="InspectLabel" type="Node" parent="Points/Entrance"]
metadata/zone = "Exit"
metadata/action = "ExitScene"
metadata/action = "Exit"
metadata/label = "Exit"
metadata/category = "Default"

Expand Down
40 changes: 25 additions & 15 deletions main_game_lib/src/cutscene/enter_an_elevator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use common_visuals::{
AtlasAnimation, AtlasAnimationEnd, AtlasAnimationStep, AtlasAnimationTimer,
BeginAtlasAnimation, EASE_IN_OUT,
};
use rscn::EntityDescription;
use top_down::layout::LAYOUT;

use crate::{
Expand Down Expand Up @@ -295,31 +296,40 @@ pub fn spawn(
/// Make sure to insert it, otherwise the player could interact with the
/// elevator and it'd be a mess.
pub fn start_with_open_elevator_and_close_it(
cmd: &mut Commands,
player: Entity,
mut elevator: EntityWorldMut,
elevator_entity: Entity,
elevator: &mut EntityDescription,
) {
// get the last frame index
let layout = elevator.get::<TextureAtlas>().unwrap().layout.clone();
let layouts = elevator
.world()
.get_resource::<Assets<TextureAtlasLayout>>()
.unwrap();
let last_frame = layouts.get(&layout).unwrap().textures.len() - 1;
trace!("Setting elevator to last frame");
let last_frame = elevator
.atlas_animation
.as_ref()
.expect("Elevator must have AtlasAnimation")
.last;
// set the last frame as the current index
elevator.get_mut::<TextureAtlas>().unwrap().index = last_frame;
elevator
.texture_atlas
.as_mut()
.expect("Elevator must have TextureAtlas")
.index = last_frame;
// start the animation as soon as we are in running state
fn is_in_running_global_state(w: &World, _: Entity) -> bool {
w.get_resource::<State<InTopDownScene>>()
.is_some_and(|scene| scene.is_running())
}
elevator.insert(common_visuals::BeginAtlasAnimation::run(
is_in_running_global_state,
from_millis(150),
Some(from_millis(1500)),
));
cmd.entity(elevator_entity).insert(
common_visuals::BeginAtlasAnimation::run(
is_in_running_global_state,
from_millis(150),
Some(from_millis(1500)),
),
);

let mut a = elevator.get_mut::<AtlasAnimation>().unwrap();
let a = elevator
.atlas_animation
.as_mut()
.expect("Elevator must have AtlasAnimation");
// animation runs in reverse
a.play = AtlasAnimationStep::Backward;
// on last frame, put everything back to normal
Expand Down
31 changes: 25 additions & 6 deletions main_game_lib/src/rscn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ use bevy::{
use common_ext::QueryExt;
pub use loader::{LoaderError, TscnLoader};
use serde::{Deserialize, Serialize};
pub use spawner::TscnSpawner;
pub use spawner::{EntityDescription, EntityDescriptionMap, TscnSpawnHooks};

/// A helper component that is always in an entity with
/// [`bevy::prelude::SpatialBundle`].
/// [bevy::prelude::SpatialBundle].
///
/// Translated a simple point from Godot.
/// To add this component, add a child Godot `Node` named `Point` to a parent
Expand Down Expand Up @@ -74,21 +74,21 @@ pub struct Config {
/// We are very selective about what we support.
/// We panic on unsupported content aggressively.
///
/// See [`parse`] and [`TscnTree::spawn_into`].
/// See [parse] and [TscnTree::spawn_into].
#[derive(Asset, TypePath, Debug, PartialEq, Serialize, Deserialize)]
pub struct TscnTree {
/// The root node of the scene as defined in Godot.
pub root_node_name: NodeName,
/// Other nodes refer to it as `"."`.
pub root: Node,
pub root: RscnNode,
}

/// Node's name is stored in the parent node's children map.
///
/// The convention is that a 2D node is an entity while a plain node is a
/// component.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Node {
pub struct RscnNode {
/// Positional data is relevant for
/// - `Node2D`
/// - `Sprite2D`
Expand All @@ -108,7 +108,7 @@ pub struct Node {
/// data. Otherwise, they are treated as components and not entities.
#[serde(skip_serializing_if = "HashMap::is_empty")]
#[serde(default)]
pub children: HashMap<NodeName, Node>,
pub children: HashMap<NodeName, RscnNode>,
}

/// The name of a node is unique within its parent.
Expand Down Expand Up @@ -249,6 +249,13 @@ impl Default for Config {
}
}

impl NodeName {
/// Get the name as a [str].
pub fn as_str(&self) -> &str {
&self.0
}
}

impl<'a> From<NodeName> for Cow<'a, str> {
fn from(NodeName(name): NodeName) -> Self {
Cow::Owned(name)
Expand All @@ -260,3 +267,15 @@ impl std::borrow::Borrow<str> for NodeName {
&self.0
}
}

impl From<NodeName> for String {
fn from(NodeName(name): NodeName) -> Self {
name
}
}

impl From<String> for NodeName {
fn from(name: String) -> Self {
NodeName(name)
}
}
Loading

0 comments on commit 614823b

Please sign in to comment.