Skip to content

Commit

Permalink
fix laser
Browse files Browse the repository at this point in the history
  • Loading branch information
bgkillas committed Jan 17, 2025
1 parent 5ef6bab commit 527acb9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
22 changes: 16 additions & 6 deletions ewext/noita_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ impl EntityID {
.ok_or_else(|| eyre!("Entity {self:?} has no component {}", C::NAME_STR))
}

pub fn get_first_component_including_disabled<C: Component>(
self,
tag: Option<Cow<'_, str>>,
) -> eyre::Result<C> {
self.try_get_first_component_including_disabled(tag)?
.ok_or_else(|| eyre!("Entity {self:?} has no component {}", C::NAME_STR))
}

pub fn remove_all_components_of_type<C: Component>(self) -> eyre::Result<()> {
while let Some(c) = self.try_get_first_component::<C>(None)? {
raw::entity_remove_component(self, c.into())?;
Expand All @@ -137,10 +145,12 @@ impl EntityID {
self,
tag: Option<Cow<'_, str>>,
) -> eyre::Result<impl Iterator<Item = C>> {
Ok(raw::entity_get_component_including_disabled(self, C::NAME_STR.into(), tag)?
.unwrap_or_default()
.into_iter()
.filter_map(|x| x.map(C::from)))
Ok(
raw::entity_get_component_including_disabled(self, C::NAME_STR.into(), tag)?
.unwrap_or_default()
.into_iter()
.filter_map(|x| x.map(C::from)),
)
}

pub fn add_component<C: Component>(self) -> eyre::Result<C> {
Expand Down Expand Up @@ -472,7 +482,7 @@ pub mod raw {
lua.push_string(object);
lua.push_string(field);
value.put(lua);
lua.call((2 + T::SIZE_ON_STACK).try_into()?, 0)
lua.call((3 + T::SIZE_ON_STACK).try_into()?, 0)
.wrap_err("Failed to call ComponentObjectSetValue2")?;
Ok(())
}
Expand Down Expand Up @@ -526,4 +536,4 @@ pub struct PhysData {
pub vx: f32,
pub vy: f32,
pub av: f32,
}
}
60 changes: 32 additions & 28 deletions ewext/src/modules/entity_sync/diff_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::NetManager;
use crate::{modules::ModuleCtx, my_peer_id, print_error, ExtState};
use crate::{modules::ModuleCtx, my_peer_id, print_error};
use bimap::BiHashMap;
use eyre::{Context, ContextCompat, OptionExt};
use noita_api::raw::{entity_create_new, raytrace_platforms};
Expand Down Expand Up @@ -163,7 +163,9 @@ impl LocalDiffModelTracker {
.filter_map(|ent| {
if ent.has_tag("touchmagic_immunity") {
let var = ent
.get_first_component::<VariableStorageComponent>(None)
.get_first_component_including_disabled::<VariableStorageComponent>(
None,
)
.ok()?;
Some(1 << var.value_int().ok()?)
} else {
Expand Down Expand Up @@ -258,17 +260,21 @@ impl LocalDiffModelTracker {
}

info.laser = None;
if !entity
.iter_all_components_of_type::<SpriteComponent>(Some("laser_sight".into()))?
.collect::<Vec<SpriteComponent>>()
.is_empty()
if entity
.try_get_first_component::<SpriteComponent>(Some("laser_sight".into()))?
.is_some()
&& &entity.name()? != "$animal_turret"
{
let ai = entity.get_first_component::<AnimalAIComponent>(None)?;
if let Some(target) = ai.m_greatest_prey()? {
if let Ok(var) = target
.get_first_component::<VariableStorageComponent>(Some("ew_peer_id".into()))
game_print(target.0.to_string());
if let Some(var) = target
.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(
None,
)?
.find(|var| var.name().unwrap_or("".into()) == "ew_peer_id")
{
game_print(var.value_string()?);
info.laser = Some(PeerId(var.value_string()?.parse::<u64>()?))
}
}
Expand Down Expand Up @@ -829,7 +835,7 @@ impl RemoteDiffModel {
for ent in entity.children(None) {
if ent.has_tag("touchmagic_immunity") {
if let Ok(var) =
ent.get_first_component::<VariableStorageComponent>(None)
ent.get_first_component_including_disabled::<VariableStorageComponent>(None)
{
if let Ok(n) = var.value_int() {
if (entity_info.mom_orbs & (1 << (n as u8))) == 0 {
Expand Down Expand Up @@ -1065,26 +1071,24 @@ impl RemoteDiffModel {
laser.object_set_value::<i32>("laser", "beam_particle_type", 225)?;
laser
};
ExtState::with_global(|state| {
if let Some(ent) = state.player_entity_map.get_by_left(&peer) {
let (x, y) = entity.position()?;
let (tx, ty) = ent.position()?;
if !raytrace_platforms(x as f64, y as f64, tx as f64, ty as f64)?.0
{
laser.set_is_emitting(true)?;
let (dx, dy) = (tx - x, ty - y);
let theta = dy.atan2(dx);
laser.set_laser_angle_add_rad(theta - entity.rotation()?)?;
laser.object_set_value::<f32>(
"laser",
"max_length",
dx.hypot(dy),
)?;
}
if let Some(ent) = ctx.player_map.get_by_left(&peer) {
game_print(ent.0.to_string());
let (x, y) = entity.position()?;
let (tx, ty) = ent.position()?;
if !raytrace_platforms(x as f64, y as f64, tx as f64, ty as f64)?.0 {
laser.set_is_emitting(true)?;
let (dx, dy) = (tx - x, ty - y);
let theta = dy.atan2(dx);
laser.set_laser_angle_add_rad(theta - entity.rotation()?)?;
laser.object_set_value::<f32>(
"laser",
"max_length",
dx.hypot(dy),
)?;
} else {
laser.set_is_emitting(false)?;
}
let a: eyre::Result<()> = Ok(());
a
})??;
}
} else if let Some(laser) = laser {
laser.set_is_emitting(false)?;
}
Expand Down
1 change: 1 addition & 0 deletions quant.ew/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ function OnPlayerSpawned(player_entity) -- This runs when player entity has been
ctx.player_data_by_local_entity[player_entity] = my_player
ctx.ready = true
ctx.my_player = my_player
EntityAddComponent2(player_entity, "VariableStorageComponent", { name = "ew_peer_id", value_string = ctx.my_id })

EntityAddTag(player_entity, "ew_peer")

Expand Down
2 changes: 1 addition & 1 deletion shared/src/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ pub struct ProjectileFired {
pub position: (f32, f32),
pub target: (f32, f32),
pub serialized: Vec<u8>,
}
}

0 comments on commit 527acb9

Please sign in to comment.