DEV-415 looking for enemy in both side
This commit is contained in:
parent
c17da1cfd8
commit
7d3c3ec092
|
|
@ -1,8 +1,10 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use crate::msg::Message;
|
||||
use crate::Enemy;
|
||||
use fyrox::{
|
||||
core::{
|
||||
algebra::{Vector2, Vector3, Point2},
|
||||
algebra::{Point2, Vector2, Vector3},
|
||||
color::Color,
|
||||
impl_component_provider,
|
||||
log::Log,
|
||||
|
|
@ -14,9 +16,9 @@ use fyrox::{
|
|||
event::{ElementState, Event, TouchPhase, WindowEvent},
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
scene::{
|
||||
collider::InteractionGroups,
|
||||
animation::spritesheet::SpriteSheetAnimation,
|
||||
base::BaseBuilder,
|
||||
collider::InteractionGroups,
|
||||
dim2::{
|
||||
physics::{Intersection, RayCastOptions},
|
||||
rectangle::{Rectangle, RectangleBuilder},
|
||||
|
|
@ -25,10 +27,8 @@ use fyrox::{
|
|||
node::Node,
|
||||
transform::TransformBuilder,
|
||||
},
|
||||
script::{ScriptContext, ScriptTrait, ScriptMessageContext, ScriptMessagePayload},
|
||||
script::{ScriptContext, ScriptMessageContext, ScriptMessagePayload, ScriptTrait},
|
||||
};
|
||||
use crate::msg::Message;
|
||||
use crate::Enemy;
|
||||
|
||||
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
||||
pub struct Player {
|
||||
|
|
@ -158,7 +158,7 @@ impl Player {
|
|||
}
|
||||
|
||||
pub fn take_damage(&mut self, damage: &f32) {
|
||||
if self.health == 0.0 {
|
||||
if self.health == 0.0 {
|
||||
return;
|
||||
}
|
||||
self.health -= damage;
|
||||
|
|
@ -326,37 +326,43 @@ impl Player {
|
|||
let self_position = self_node.global_position();
|
||||
// Cast a ray from *this* node in the direction of the player node
|
||||
let mut buffer = Vec::<Intersection>::new();
|
||||
|
||||
context.scene.graph.physics2d.cast_ray(
|
||||
RayCastOptions {
|
||||
ray_origin: Point2::new(self_position.x, self_position.y),
|
||||
ray_direction: Vector2::new(-1.0, 0.0),
|
||||
max_len: 1.0,
|
||||
groups: InteractionGroups::default(),
|
||||
// groups: InteractionGroups::default(),
|
||||
// Sort the results by distance
|
||||
sort_results: true,
|
||||
},
|
||||
// Store the collisions in the vector
|
||||
&mut buffer,
|
||||
);
|
||||
if buffer.len() == 0 {
|
||||
return ;
|
||||
let x_coord: [f32; 2] = [-1.0, 1.0];
|
||||
for x in x_coord.iter() {
|
||||
context.scene.graph.physics2d.cast_ray(
|
||||
RayCastOptions {
|
||||
ray_origin: Point2::new(self_position.x, self_position.y),
|
||||
ray_direction: Vector2::new(x.clone(), 0.0),
|
||||
max_len: 1.0,
|
||||
groups: InteractionGroups::default(),
|
||||
// groups: InteractionGroups::default(),
|
||||
// Sort the results by distance
|
||||
sort_results: true,
|
||||
},
|
||||
// Store the collisions in the vector
|
||||
&mut buffer,
|
||||
);
|
||||
if buffer.len() == 1 {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
for i in 0..buffer.len() {
|
||||
if buffer[i].toi == 0.0 {
|
||||
continue;
|
||||
}
|
||||
if let Some(d) = context.scene.graph.try_get(buffer[i].collider) {
|
||||
match context.scene.graph.try_get_script_of_mut::<Enemy>(d.parent()) {
|
||||
Some(script) => {
|
||||
script.take_damage(&self.damage);
|
||||
break;
|
||||
}
|
||||
None => {}
|
||||
for i in 0..buffer.len() {
|
||||
if buffer[i].toi == 0.0 {
|
||||
continue;
|
||||
}
|
||||
if let Some(d) = context.scene.graph.try_get(buffer[i].collider) {
|
||||
match context
|
||||
.scene
|
||||
.graph
|
||||
.try_get_script_of_mut::<Enemy>(d.parent())
|
||||
{
|
||||
Some(script) => {
|
||||
script.take_damage(&self.damage);
|
||||
break;
|
||||
}
|
||||
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -470,7 +476,9 @@ impl ScriptTrait for Player {
|
|||
|
||||
// Put start logic - it is called when every other script is already initialized.
|
||||
fn on_start(&mut self, context: &mut ScriptContext) {
|
||||
context.message_dispatcher.subscribe_to::<Message>(context.handle);
|
||||
context
|
||||
.message_dispatcher
|
||||
.subscribe_to::<Message>(context.handle);
|
||||
}
|
||||
|
||||
fn on_message(
|
||||
|
|
|
|||
Loading…
Reference in New Issue