-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
655 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod objects; | ||
mod particles; | ||
mod spawner; | ||
mod world; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod building; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
use godot::{ | ||
builtin::{Array, NodePath, Transform3D, Vector3}, | ||
engine::{MeshInstance3D, Node, Node3D, PackedScene, ResourceLoader, Time}, | ||
meta::ToGodot, | ||
obj::{Gd, Inherits}, | ||
}; | ||
use godot_rust_script::{godot_script_impl, Context, GodotScript}; | ||
use rand::Rng; | ||
use std::fmt::Debug; | ||
|
||
use crate::{util::logger, world::city_data::TileCoords}; | ||
|
||
trait BuildingFeature<N: Inherits<Node>>: Debug { | ||
fn process(&mut self, _delta: f64, _node: &mut Gd<N>) {} | ||
fn physics_process(&mut self, _delta: f64, _node: &mut Gd<N>) {} | ||
} | ||
|
||
struct BuildingEventFlags(u8); | ||
|
||
impl BuildingEventFlags { | ||
fn fire(&self) -> bool { | ||
self.0 & 0b00000001 == 1 | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Features<F: Debug + ?Sized>(Vec<Box<F>>); | ||
|
||
impl<F: Debug + ?Sized> Features<F> { | ||
pub fn push(&mut self, feature: Box<F>) { | ||
self.0.push(feature); | ||
} | ||
} | ||
|
||
impl<F: Debug + ?Sized> Default for Features<F> { | ||
fn default() -> Self { | ||
Self(Vec::default()) | ||
} | ||
} | ||
|
||
impl<N: Inherits<Node>> BuildingFeature<N> for Features<dyn BuildingFeature<N>> { | ||
fn process(&mut self, delta: f64, node: &mut Gd<N>) { | ||
for item in self.0.iter_mut() { | ||
item.process(delta, node); | ||
} | ||
} | ||
|
||
fn physics_process(&mut self, delta: f64, node: &mut Gd<N>) { | ||
for item in self.0.iter_mut() { | ||
item.physics_process(delta, node); | ||
} | ||
} | ||
} | ||
|
||
const FIRE_SPAWNER_SCENE: &str = "res://resources/Objects/Spawner/fire_spawner.tscn"; | ||
|
||
#[derive(Debug)] | ||
struct FireFeature { | ||
fire_scene: Option<Gd<Node3D>>, | ||
last_fire: u64, | ||
building_mesh: Gd<MeshInstance3D>, | ||
tile_coords: TileCoords, | ||
} | ||
|
||
impl FireFeature { | ||
fn new(tile_coords: TileCoords, mesh: &Gd<MeshInstance3D>) -> Self { | ||
Self { | ||
fire_scene: None, | ||
last_fire: 0, | ||
building_mesh: mesh.to_owned(), | ||
tile_coords, | ||
} | ||
} | ||
} | ||
|
||
impl<N: Inherits<Node>> BuildingFeature<N> for FireFeature { | ||
fn process(&mut self, _delta: f64, node: &mut Gd<N>) { | ||
let current_ticks = Time::singleton().get_ticks_msec(); | ||
|
||
if let Some(ref mut scene) = self.fire_scene { | ||
if current_ticks - self.last_fire < 60_000 { | ||
return; | ||
} | ||
|
||
scene.queue_free(); | ||
self.fire_scene = None; | ||
return; | ||
} | ||
|
||
let tick_delta = current_ticks - self.last_fire; | ||
let tick_boost = (tick_delta + 10_000) as f64 / 10_000.0; | ||
let rng = rand::thread_rng().gen_range(0.0..1.0); | ||
|
||
let chance = rng * tick_boost; | ||
|
||
if chance < 0.5 { | ||
return; | ||
} | ||
|
||
let Some(scene) = ResourceLoader::singleton() | ||
.load_ex(FIRE_SPAWNER_SCENE.into()) | ||
.type_hint("PackedScene".into()) | ||
.done() | ||
else { | ||
logger::error!("Failed to load fire_spawner scene: {}", FIRE_SPAWNER_SCENE); | ||
return; | ||
}; | ||
|
||
let Some(mut scene_instance) = scene.cast::<PackedScene>().try_instantiate_as::<Node3D>() | ||
else { | ||
logger::error!( | ||
"Failed to instantiate fire_spawner scene as decendant of Node3D: {}", | ||
FIRE_SPAWNER_SCENE | ||
); | ||
return; | ||
}; | ||
|
||
let aabb = self.building_mesh.get_aabb(); | ||
let aabb_size = (Transform3D::new(self.building_mesh.get_basis(), Vector3::default()) | ||
* aabb.size) | ||
.abs(); | ||
|
||
scene_instance.call_deferred("resize".into(), &[aabb_size.to_variant()]); | ||
|
||
node.upcast_mut() | ||
.add_child_ex(scene_instance.clone().upcast()) | ||
.force_readable_name(true) | ||
.done(); | ||
|
||
self.fire_scene = Some(scene_instance); | ||
self.last_fire = current_ticks; | ||
|
||
logger::info!("Building started burning: {:?}", self.tile_coords); | ||
} | ||
} | ||
|
||
#[derive(GodotScript, Debug)] | ||
#[script(base = Node)] | ||
struct Building { | ||
#[export(flags = ["Fire:1"])] | ||
pub events: u8, | ||
|
||
#[export(node_path = ["MeshInstance3D"])] | ||
pub mesh_path: NodePath, | ||
|
||
pub tile_coords_array: Array<u32>, | ||
|
||
tile_coords: TileCoords, | ||
|
||
mesh: Option<Gd<MeshInstance3D>>, | ||
features: Features<dyn BuildingFeature<Node>>, | ||
|
||
base: Gd<Node>, | ||
} | ||
|
||
#[godot_script_impl] | ||
impl Building { | ||
pub fn _ready(&mut self, mut context: Context) { | ||
let events = BuildingEventFlags(self.events); | ||
|
||
self.mesh = { | ||
let mesh_path = self.mesh_path.clone(); | ||
let base = self.base.clone(); | ||
|
||
context.reentrant_scope(self, || base.try_get_node_as(mesh_path.to_owned())) | ||
}; | ||
|
||
self.tile_coords = ( | ||
self.tile_coords_array.get(0).unwrap_or(0), | ||
self.tile_coords_array.get(1).unwrap_or(0), | ||
); | ||
|
||
if events.fire() { | ||
if let Some(ref mesh) = self.mesh { | ||
self.features | ||
.push(Box::new(FireFeature::new(self.tile_coords, mesh))); | ||
} else { | ||
logger::warn!("Unable to instantiate FireFeature because no mesh has been set."); | ||
} | ||
} | ||
} | ||
|
||
pub fn _process(&mut self, delta: f64) { | ||
self.features.process(delta, &mut self.base); | ||
} | ||
|
||
pub fn _physics_process(&mut self, delta: f64) { | ||
self.features.physics_process(delta, &mut self.base); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use godot::{ | ||
builtin::{NodePath, Transform3D, Vector3, Vector3Axis}, | ||
engine::{light_3d, AnimationPlayer, FogVolume, Node, OmniLight3D}, | ||
obj::Gd, | ||
}; | ||
use godot_rust_script::{godot_script_impl, GodotScript}; | ||
|
||
use crate::util::logger; | ||
|
||
#[derive(GodotScript, Debug)] | ||
#[script(base = Node)] | ||
struct FireSpawner { | ||
#[export(node_path = ["FogVolume"])] | ||
pub fire_path: NodePath, | ||
fire: Option<Gd<FogVolume>>, | ||
|
||
#[export(node_path = ["FogVolume"])] | ||
pub smoke_path: NodePath, | ||
smoke: Option<Gd<FogVolume>>, | ||
|
||
#[export(node_path = ["OmniLight3D"])] | ||
pub light_source_path: NodePath, | ||
light_source: Option<Gd<OmniLight3D>>, | ||
|
||
#[export(node_path = ["AnimationPlayer"])] | ||
pub audio_player_path: NodePath, | ||
audio_player: Option<Gd<AnimationPlayer>>, | ||
|
||
base: Gd<Node>, | ||
} | ||
|
||
#[godot_script_impl] | ||
impl FireSpawner { | ||
pub fn _ready(&mut self) { | ||
logger::debug!("Init Fire spawner..."); | ||
self.fire = self.base.try_get_node_as(self.fire_path.clone()); | ||
self.smoke = self.base.try_get_node_as(self.smoke_path.clone()); | ||
self.audio_player = self.base.try_get_node_as(self.audio_player_path.clone()); | ||
self.light_source = self.base.try_get_node_as(self.light_source_path.clone()); | ||
|
||
if let Some(ref mut audio_player) = self.audio_player { | ||
audio_player.set_current_animation("burning".into()); | ||
} else { | ||
logger::warn!("No audio player has been setup!"); | ||
} | ||
} | ||
|
||
pub fn resize(&mut self, size: Vector3) { | ||
logger::debug!("Resizing fire spawner..."); | ||
let Some(ref mut fire) = self.fire else { | ||
logger::error!("Failed to resize fire spawner! No fire setup!"); | ||
return; | ||
}; | ||
|
||
let Some(ref mut smoke) = self.smoke else { | ||
logger::error!("Failed to resize fire spawner! No smoke setup!"); | ||
return; | ||
}; | ||
|
||
let Some(ref mut light_source) = self.light_source else { | ||
logger::error!("Failed to resize fire spawner! No light source setup!"); | ||
return; | ||
}; | ||
|
||
let smoke_ratio = smoke.get_size() / fire.get_size(); | ||
let fire_size = size * Vector3::new(1.0, 1.5, 1.0); | ||
let smoke_size = fire_size * smoke_ratio; | ||
let light_size = size; | ||
|
||
fire.set_size(fire_size); | ||
fire.set_transform(Transform3D::default().translated(Vector3::new( | ||
0.0, | ||
fire_size.y / 2.0 * 0.9, | ||
0.0, | ||
))); | ||
|
||
smoke.set_size(smoke_size); | ||
smoke.set_transform(Transform3D::default().translated(Vector3::new( | ||
0.0, | ||
smoke_size.y / 2.0 * 1.2, | ||
0.0, | ||
))); | ||
|
||
let light_max_size = match light_size.max_axis().unwrap_or(Vector3Axis::X) { | ||
Vector3Axis::X => light_size.x, | ||
Vector3Axis::Y => light_size.y, | ||
Vector3Axis::Z => light_size.z, | ||
}; | ||
|
||
light_source.set_param(light_3d::Param::RANGE, light_size.length_squared()); | ||
light_source.set_param(light_3d::Param::SIZE, light_max_size); | ||
light_source.set_transform(Transform3D::default().translated(Vector3::new( | ||
0.0, | ||
light_size.y / 2.0, | ||
0.0, | ||
))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod car_spawner; | ||
mod fire_spawner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.