enemy can take damage
This commit is contained in:
parent
6ef12b6332
commit
85d4e5c658
|
|
@ -33,6 +33,7 @@ enum Animations {
|
||||||
Run = 1,
|
Run = 1,
|
||||||
Fight = 2,
|
Fight = 2,
|
||||||
Dead = 3,
|
Dead = 3,
|
||||||
|
Block = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DISTANCE_TO_FIGHT: f32 = 0.5;
|
const DISTANCE_TO_FIGHT: f32 = 0.5;
|
||||||
|
|
@ -50,6 +51,7 @@ pub struct Enemy {
|
||||||
fight: bool,
|
fight: bool,
|
||||||
win: bool,
|
win: bool,
|
||||||
dead: bool,
|
dead: bool,
|
||||||
|
block: bool,
|
||||||
|
|
||||||
#[reflect(hidden)]
|
#[reflect(hidden)]
|
||||||
#[visit(skip)]
|
#[visit(skip)]
|
||||||
|
|
@ -75,6 +77,10 @@ impl Enemy {
|
||||||
return a.round() as i32;
|
return a.round() as i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn take_damage(&mut self, _damage: &f32) {
|
||||||
|
self.block = true;
|
||||||
|
}
|
||||||
|
|
||||||
fn intersections_box(&self, graph: &mut Graph) -> bool {
|
fn intersections_box(&self, graph: &mut Graph) -> bool {
|
||||||
let self_node = match graph.try_get(self.handle) {
|
let self_node = match graph.try_get(self.handle) {
|
||||||
Some(node) => node,
|
Some(node) => node,
|
||||||
|
|
@ -175,9 +181,11 @@ impl Enemy {
|
||||||
Some(script) => {
|
Some(script) => {
|
||||||
if script.get_health() <= 0.0 {
|
if script.get_health() <= 0.0 {
|
||||||
self.fight = false;
|
self.fight = false;
|
||||||
|
self.block = false;
|
||||||
self.win = true;
|
self.win = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
self.block = false;
|
||||||
script.take_damage(&self.attack_damage);
|
script.take_damage(&self.attack_damage);
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
|
|
@ -284,6 +292,9 @@ impl Enemy {
|
||||||
|
|
||||||
fn move_do(&mut self, context: &mut ScriptContext) {
|
fn move_do(&mut self, context: &mut ScriptContext) {
|
||||||
let x_speed = self.get_speed(context);
|
let x_speed = self.get_speed(context);
|
||||||
|
if x_speed > 0.0 {
|
||||||
|
self.block = false;
|
||||||
|
}
|
||||||
let enemy_rigid_body = context.scene.graph[context.handle].cast_mut::<RigidBody>();
|
let enemy_rigid_body = context.scene.graph[context.handle].cast_mut::<RigidBody>();
|
||||||
if let Some(enemy_rigid_data) = enemy_rigid_body {
|
if let Some(enemy_rigid_data) = enemy_rigid_body {
|
||||||
enemy_rigid_data.set_lin_vel(Vector2::new(x_speed, enemy_rigid_data.lin_vel().y));
|
enemy_rigid_data.set_lin_vel(Vector2::new(x_speed, enemy_rigid_data.lin_vel().y));
|
||||||
|
|
@ -292,14 +303,21 @@ impl Enemy {
|
||||||
|
|
||||||
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
||||||
self.current_animation = Animations::Idle;
|
self.current_animation = Animations::Idle;
|
||||||
if self.get_speed(context) != 0.0 {
|
if self.block {
|
||||||
self.current_animation = Animations::Run;
|
self.current_animation = Animations::Block;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if self.fight {
|
if self.fight {
|
||||||
self.current_animation = Animations::Fight;
|
self.current_animation = Animations::Fight;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if self.dead {
|
if self.dead {
|
||||||
self.current_animation = Animations::Dead;
|
self.current_animation = Animations::Dead;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if self.get_speed(context) != 0.0 {
|
||||||
|
self.current_animation = Animations::Run;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -310,6 +328,9 @@ impl Enemy {
|
||||||
if self.dead {
|
if self.dead {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if self.block {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let distance = self.distance_to_player(context);
|
let distance = self.distance_to_player(context);
|
||||||
self.fight = distance < DISTANCE_TO_FIGHT && distance > -1.0 * DISTANCE_TO_FIGHT;
|
self.fight = distance < DISTANCE_TO_FIGHT && distance > -1.0 * DISTANCE_TO_FIGHT;
|
||||||
if !self.fight {
|
if !self.fight {
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,27 @@ impl EnemySpawn {
|
||||||
return EnemySpawn::default();
|
return EnemySpawn::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn block_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
|
||||||
|
let idle = resource_manager.request::<Texture>(
|
||||||
|
"assets/data/characters/skeleton/Skeleton_Warrior/Protect.png".to_owned(),
|
||||||
|
);
|
||||||
|
let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters {
|
||||||
|
width: 128,
|
||||||
|
height: 128,
|
||||||
|
frame_width: 128,
|
||||||
|
frame_height: 128,
|
||||||
|
first_frame: 0,
|
||||||
|
last_frame: 1,
|
||||||
|
column_major: false,
|
||||||
|
});
|
||||||
|
idle_animation.set_texture(Some(idle));
|
||||||
|
idle_animation.set_looping(false);
|
||||||
|
idle_animation.set_speed(20.);
|
||||||
|
idle_animation.play();
|
||||||
|
return idle_animation;
|
||||||
|
}
|
||||||
|
|
||||||
fn attack_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
|
fn attack_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
|
||||||
let idle = resource_manager.request::<Texture>(
|
let idle = resource_manager.request::<Texture>(
|
||||||
"assets/data/characters/skeleton/Skeleton_Warrior/Attack_3.png".to_owned(),
|
"assets/data/characters/skeleton/Skeleton_Warrior/Attack_3.png".to_owned(),
|
||||||
|
|
@ -134,6 +155,7 @@ impl EnemySpawn {
|
||||||
self.run_anmation(resource_manager),
|
self.run_anmation(resource_manager),
|
||||||
self.attack_anmation(resource_manager),
|
self.attack_anmation(resource_manager),
|
||||||
self.die_anmation(resource_manager),
|
self.die_anmation(resource_manager),
|
||||||
|
self.block_anmation(resource_manager),
|
||||||
];
|
];
|
||||||
material
|
material
|
||||||
.set_property(
|
.set_property(
|
||||||
|
|
@ -146,7 +168,7 @@ impl EnemySpawn {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let shape = ColliderShape::Capsule(CapsuleShape {
|
let shape = ColliderShape::Capsule(CapsuleShape {
|
||||||
begin: Vector2::new(0.0, -0.80),
|
begin: Vector2::new(0.0, -0.80),
|
||||||
end: Vector2::new(0.0, -0.40),
|
end: Vector2::new(0.0, -0.35),
|
||||||
radius: 0.2,
|
radius: 0.2,
|
||||||
});
|
});
|
||||||
let rb_transform = TransformBuilder::new()
|
let rb_transform = TransformBuilder::new()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::collections::VecDeque;
|
||||||
|
|
||||||
use fyrox::{
|
use fyrox::{
|
||||||
core::{
|
core::{
|
||||||
algebra::{Vector2, Vector3},
|
algebra::{Vector2, Vector3, Point2},
|
||||||
color::Color,
|
color::Color,
|
||||||
impl_component_provider,
|
impl_component_provider,
|
||||||
log::Log,
|
log::Log,
|
||||||
|
|
@ -14,9 +14,11 @@ use fyrox::{
|
||||||
event::{ElementState, Event, TouchPhase, WindowEvent},
|
event::{ElementState, Event, TouchPhase, WindowEvent},
|
||||||
keyboard::{KeyCode, PhysicalKey},
|
keyboard::{KeyCode, PhysicalKey},
|
||||||
scene::{
|
scene::{
|
||||||
|
collider::InteractionGroups,
|
||||||
animation::spritesheet::SpriteSheetAnimation,
|
animation::spritesheet::SpriteSheetAnimation,
|
||||||
base::BaseBuilder,
|
base::BaseBuilder,
|
||||||
dim2::{
|
dim2::{
|
||||||
|
physics::{Intersection, RayCastOptions},
|
||||||
rectangle::{Rectangle, RectangleBuilder},
|
rectangle::{Rectangle, RectangleBuilder},
|
||||||
rigidbody::RigidBody,
|
rigidbody::RigidBody,
|
||||||
},
|
},
|
||||||
|
|
@ -26,6 +28,7 @@ use fyrox::{
|
||||||
script::{ScriptContext, ScriptTrait, ScriptMessageContext, ScriptMessagePayload},
|
script::{ScriptContext, ScriptTrait, ScriptMessageContext, ScriptMessagePayload},
|
||||||
};
|
};
|
||||||
use crate::msg::Message;
|
use crate::msg::Message;
|
||||||
|
use crate::Enemy;
|
||||||
|
|
||||||
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
#[derive(Visit, Reflect, Debug, Clone, Default)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
|
|
@ -43,6 +46,7 @@ pub struct Player {
|
||||||
start_y_position: f32,
|
start_y_position: f32,
|
||||||
start_fight_last: f32,
|
start_fight_last: f32,
|
||||||
prev_y_velosity: f32,
|
prev_y_velosity: f32,
|
||||||
|
damage: f32,
|
||||||
|
|
||||||
pub window_height: u32, // TODO: need change place
|
pub window_height: u32, // TODO: need change place
|
||||||
pub window_width: u32,
|
pub window_width: u32,
|
||||||
|
|
@ -180,9 +184,6 @@ impl Player {
|
||||||
KeyCode::ArrowRight => self.move_right = is_pressed,
|
KeyCode::ArrowRight => self.move_right = is_pressed,
|
||||||
KeyCode::ArrowUp => self.jump = is_pressed,
|
KeyCode::ArrowUp => self.jump = is_pressed,
|
||||||
KeyCode::Space => {
|
KeyCode::Space => {
|
||||||
// if (context.elapsed_time - self.start_fight_last) > 0.3 {
|
|
||||||
// self.fight = false;
|
|
||||||
// }
|
|
||||||
if !is_pressed {
|
if !is_pressed {
|
||||||
self.fight = false;
|
self.fight = false;
|
||||||
self.start_fight_last = 0.0;
|
self.start_fight_last = 0.0;
|
||||||
|
|
@ -195,23 +196,6 @@ impl Player {
|
||||||
}
|
}
|
||||||
self.start_fight_last = context.elapsed_time;
|
self.start_fight_last = context.elapsed_time;
|
||||||
self.fight = is_pressed;
|
self.fight = is_pressed;
|
||||||
// if self.fight && is_pressed {
|
|
||||||
// self.fight = false;
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// // if self.fight && !is_pressed {
|
|
||||||
// // self.start_fight_last = context.elapsed_time;
|
|
||||||
// // self.fight = false;
|
|
||||||
// // return;
|
|
||||||
// // }
|
|
||||||
// // if !self.fight && !is_pressed {
|
|
||||||
// // self.fight = false;
|
|
||||||
// // return;
|
|
||||||
// // }
|
|
||||||
// if !self.fight && is_pressed {
|
|
||||||
// self.fight = true;
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
@ -330,6 +314,52 @@ impl Player {
|
||||||
None => self.current_animation.push_front(animation as u32),
|
None => self.current_animation.push_front(animation as u32),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn attack_enemy(&self, context: &mut ScriptContext) {
|
||||||
|
if !self.fight {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let self_node = match context.scene.graph.try_get(context.handle) {
|
||||||
|
Some(node) => node,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
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 ;
|
||||||
|
}
|
||||||
|
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 => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
fn animate_choose(&mut self, context: &mut ScriptContext) {
|
||||||
if self.health <= 0.0 {
|
if self.health <= 0.0 {
|
||||||
return;
|
return;
|
||||||
|
|
@ -510,7 +540,7 @@ impl ScriptTrait for Player {
|
||||||
self.change_orientation(context);
|
self.change_orientation(context);
|
||||||
self.animation_do(context);
|
self.animation_do(context);
|
||||||
self.update_health(context);
|
self.update_health(context);
|
||||||
|
self.attack_enemy(context);
|
||||||
self.loop_over(context);
|
self.loop_over(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue