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::{
animation::spritesheet::SpriteSheetAnimation,
base::BaseBuilder,
collider::{BitMask, Collider, InteractionGroups},
collider::{BitMask, InteractionGroups},
dim2::{
physics::{Intersection, RayCastOptions},
rectangle::{Rectangle, RectangleBuilder},
rectangle::Rectangle,
rigidbody::RigidBody,
},
graph::Graph,
node::Node,
transform::TransformBuilder,
},
script::{ScriptContext, ScriptTrait},
};
@ -63,36 +61,22 @@ pub struct Enemy {
attack_speed: f32,
}
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) {
self.attack_damage = 2.0;
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
let self_node = match graph.try_get(self.handle) {
Some(node) => node,
None => return,
None => return None,
};
// Get the player node
let player_node = match graph.try_get(self.player_handle) {
Some(node) => node,
None => return,
None => return None,
};
// Get the current position of *this* node and the player node
@ -109,32 +93,43 @@ impl Enemy {
RayCastOptions {
ray_origin: Point2::new(self_position.x, self_position.y),
ray_direction: Vector2::new(direction.x, direction.y),
max_len: 1.,
max_len: max_len,
groups: InteractionGroups::new(
// Only collide with the player
BitMask(0b1000_0000_0000_0000_0000_0000_0000_0000),
BitMask(0b0111_1111_1111_1111_1111_1111_1111_1111),
),
// groups: InteractionGroups::default(),
// Sort the results by distance
sort_results: true,
},
// Store the collisions in the vector
&mut buffer,
);
// If the ray hit the player, damage the player
for i in 0..buffer.len() {
if buffer[i].collider != self.player_collider {
continue;
}
match graph.try_get_script_of_mut::<Player>(self.player_handle) {
Some(script) => {
script.take_damage(&self.attack_damage);
if buffer.len() == 0 {
return None;
}
return Some(buffer);
}
fn attack_player(&mut self, graph: &mut Graph) {
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 => {}
};
break;
match graph.try_get_script_of_mut::<Player>(self.player_handle) {
Some(script) => {
script.take_damage(&self.attack_damage);
}
None => {}
};
break;
}
}
}
fn distance_to_player(&self, context: &mut ScriptContext) -> f32 {
let mut graph_ctx = context.scene.graph.begin_multi_borrow::<2>();
let enemy_rigid_body = graph_ctx
@ -165,6 +160,24 @@ impl Enemy {
}
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);
if distance > DISTANCE_TO_VIEW {
return 0.0;