player can hide from skeleton

This commit is contained in:
artem 2024-02-01 14:29:13 +03:00
parent 29b3f57986
commit c8acf41c09
1 changed files with 46 additions and 33 deletions

View File

@ -9,16 +9,14 @@ use fyrox::{
}, },
scene::{ scene::{
animation::spritesheet::SpriteSheetAnimation, animation::spritesheet::SpriteSheetAnimation,
base::BaseBuilder, collider::{BitMask, InteractionGroups},
collider::{BitMask, Collider, InteractionGroups},
dim2::{ dim2::{
physics::{Intersection, RayCastOptions}, physics::{Intersection, RayCastOptions},
rectangle::{Rectangle, RectangleBuilder}, rectangle::Rectangle,
rigidbody::RigidBody, rigidbody::RigidBody,
}, },
graph::Graph, graph::Graph,
node::Node, node::Node,
transform::TransformBuilder,
}, },
script::{ScriptContext, ScriptTrait}, script::{ScriptContext, ScriptTrait},
}; };
@ -63,36 +61,22 @@ pub struct Enemy {
attack_speed: f32, attack_speed: f32,
} }
impl Enemy { impl Enemy {
// pub fn build(&self) {
// let mut material = Material::standard_2d();
// material
// .set_property(
// &ImmutableString::new("diffuseTexture"),
// PropertyValue::Sampler {
// value: Some(resource_manager.request::<Texture>(path)),
// fallback: SamplerFallback::Normal,
// },
// )
// .unwrap();
// material_resource = MaterialResource::new_ok(Default::default(), material);
// }
fn init(&mut self) { fn init(&mut self) {
self.attack_damage = 2.0; self.attack_damage = 2.0;
self.attack_speed = 0.5; self.attack_speed = 0.5;
} }
fn attack_player(&mut self, graph: &mut Graph) { fn intersections(&self, graph: &mut Graph, max_len: f32) -> Option<Vec<Intersection>> {
// Get *this* node instance // Get *this* node instance
let self_node = match graph.try_get(self.handle) { let self_node = match graph.try_get(self.handle) {
Some(node) => node, Some(node) => node,
None => return, None => return None,
}; };
// Get the player node // Get the player node
let player_node = match graph.try_get(self.player_handle) { let player_node = match graph.try_get(self.player_handle) {
Some(node) => node, Some(node) => node,
None => return, None => return None,
}; };
// Get the current position of *this* node and the player node // Get the current position of *this* node and the player node
@ -109,32 +93,43 @@ impl Enemy {
RayCastOptions { RayCastOptions {
ray_origin: Point2::new(self_position.x, self_position.y), ray_origin: Point2::new(self_position.x, self_position.y),
ray_direction: Vector2::new(direction.x, direction.y), ray_direction: Vector2::new(direction.x, direction.y),
max_len: 1., max_len: max_len,
groups: InteractionGroups::new( groups: InteractionGroups::new(
// Only collide with the player // Only collide with the player
BitMask(0b1000_0000_0000_0000_0000_0000_0000_0000), BitMask(0b1000_0000_0000_0000_0000_0000_0000_0000),
BitMask(0b0111_1111_1111_1111_1111_1111_1111_1111), BitMask(0b0111_1111_1111_1111_1111_1111_1111_1111),
), ),
// groups: InteractionGroups::default(),
// Sort the results by distance // Sort the results by distance
sort_results: true, sort_results: true,
}, },
// Store the collisions in the vector // Store the collisions in the vector
&mut buffer, &mut buffer,
); );
// If the ray hit the player, damage the player if buffer.len() == 0 {
for i in 0..buffer.len() { return None;
if buffer[i].collider != self.player_collider { }
continue; return Some(buffer);
} }
match graph.try_get_script_of_mut::<Player>(self.player_handle) {
Some(script) => { fn attack_player(&mut self, graph: &mut Graph) {
script.take_damage(&self.attack_damage); let collaider = self.intersections(graph, 1.);
if let Some(collaider_data) = collaider {
for i in 0..collaider_data.len() {
if collaider_data[i].collider != self.player_collider {
continue;
} }
None => {} match graph.try_get_script_of_mut::<Player>(self.player_handle) {
}; Some(script) => {
break; script.take_damage(&self.attack_damage);
}
None => {}
};
break;
}
} }
} }
fn distance_to_player(&self, context: &mut ScriptContext) -> f32 { fn distance_to_player(&self, context: &mut ScriptContext) -> f32 {
let mut graph_ctx = context.scene.graph.begin_multi_borrow::<2>(); let mut graph_ctx = context.scene.graph.begin_multi_borrow::<2>();
let enemy_rigid_body = graph_ctx let enemy_rigid_body = graph_ctx
@ -165,6 +160,24 @@ impl Enemy {
} }
fn get_speed(&self, context: &mut ScriptContext) -> f32 { fn get_speed(&self, context: &mut ScriptContext) -> f32 {
let collaider = self.intersections(&mut context.scene.graph, 5.);
let mut see_player: bool = false;
if let Some(collaider_data) = collaider {
for i in 0..collaider_data.len() {
if collaider_data[i].toi == 0.0 {
continue;
}
if collaider_data[i].collider == self.player_collider {
see_player = true;
}
break;
}
}
if !see_player {
return 0.0;
}
let distance = self.distance_to_player(context); let distance = self.distance_to_player(context);
if distance > DISTANCE_TO_VIEW { if distance > DISTANCE_TO_VIEW {
return 0.0; return 0.0;