несколько багов: чтобы скелет не перекидывал ящик за край, во время бега не бил

This commit is contained in:
artem 2024-02-23 11:33:53 +03:00
parent b217bf05bf
commit 9845202780
2 changed files with 40 additions and 43 deletions

View File

@ -40,6 +40,7 @@ const DISTANCE_TO_FIGHT: f32 = 0.5;
const DISTANCE_TO_VIEW: f32 = 5.0; const DISTANCE_TO_VIEW: f32 = 5.0;
const END_MAP_LEFT: f32 = -30.0; const END_MAP_LEFT: f32 = -30.0;
const END_MAP_RIGHT: f32 = 30.0; const END_MAP_RIGHT: f32 = 30.0;
const SPEED: f32 = 3.1;
#[derive(Visit, Reflect, Debug, Clone, Default)] #[derive(Visit, Reflect, Debug, Clone, Default)]
pub struct Enemy { pub struct Enemy {
@ -180,11 +181,8 @@ impl Enemy {
if self.win { if self.win {
return; return;
} }
let collaider = self.intersections_player(graph, 1.); if !self.fight {
if let Some(collaider_data) = collaider { return;
for i in 0..collaider_data.len() {
if collaider_data[i].collider != self.player_collider {
continue;
} }
match graph.try_get_script_of_mut::<Player>(self.player_handle) { match graph.try_get_script_of_mut::<Player>(self.player_handle) {
Some(script) => { Some(script) => {
@ -192,15 +190,12 @@ impl Enemy {
self.fight = false; self.fight = false;
self.block = false; self.block = false;
self.win = true; self.win = true;
break; return;
} }
self.block = false; self.block = false;
script.take_damage(&self.attack_damage); script.take_damage(&self.attack_damage);
} }
None => {} None => {}
};
break;
}
} }
} }
@ -266,10 +261,10 @@ impl Enemy {
} }
if distance > DISTANCE_TO_FIGHT { if distance > DISTANCE_TO_FIGHT {
return 3.1; return SPEED;
} }
if distance < DISTANCE_TO_FIGHT * -1.0 { if distance < DISTANCE_TO_FIGHT * -1.0 {
return -3.1; return SPEED * -1.0;
} }
return 0.0; return 0.0;
} }
@ -319,10 +314,6 @@ impl Enemy {
self.current_animation = Animations::Block; self.current_animation = Animations::Block;
return; return;
} }
if self.fight {
self.current_animation = Animations::Fight;
return;
}
if self.dead { if self.dead {
self.current_animation = Animations::Dead; self.current_animation = Animations::Dead;
return; return;
@ -331,6 +322,10 @@ impl Enemy {
self.current_animation = Animations::Run; self.current_animation = Animations::Run;
return; return;
} }
if self.fight {
self.current_animation = Animations::Fight;
return;
}
} }
fn set_fight(&mut self, context: &mut ScriptContext) { fn set_fight(&mut self, context: &mut ScriptContext) {
@ -353,7 +348,9 @@ impl Enemy {
if self.attack_timer >= self.attack_speed { if self.attack_timer >= self.attack_speed {
self.attack_player(&mut context.scene.graph); self.attack_player(&mut context.scene.graph);
self.attack_timer = 0.0; self.attack_timer = 0.0;
return;
} }
self.fight = false;
} }
pub fn animation_do(&mut self, context: &mut ScriptContext) { pub fn animation_do(&mut self, context: &mut ScriptContext) {
@ -432,9 +429,9 @@ impl ScriptTrait for Enemy {
if self.intersections_box(&mut context.scene.graph) { if self.intersections_box(&mut context.scene.graph) {
self.dead = true self.dead = true
} }
self.set_fight(context);
self.change_orientation(context); self.change_orientation(context);
self.animation_do(context); self.animation_do(context);
self.move_do(context); self.move_do(context);
self.set_fight(context);
} }
} }

View File

@ -192,22 +192,22 @@ fn build_stone_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f
) )
} }
fn build_mushroom_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { // fn build_mushroom_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) {
let shape = ColliderShape::Cuboid(CuboidShape { // let shape = ColliderShape::Cuboid(CuboidShape {
half_extents: Vector2::new(0.25, 0.25), // half_extents: Vector2::new(0.25, 0.25),
}); // });
build_block( // build_block(
graph, // graph,
x as f32, // x as f32,
y + 0.5, // y + 0.5,
0.5, // 0.5,
build_material( // build_material(
resource_manager, // resource_manager,
"assets/data/objects/Mushroom_2.png".to_owned(), // "assets/data/objects/Mushroom_2.png".to_owned(),
), // ),
shape, // shape,
) // )
} // }
fn build_box_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { fn build_box_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) {
let shape = ColliderShape::Cuboid(CuboidShape::default()); let shape = ColliderShape::Cuboid(CuboidShape::default());
@ -235,7 +235,7 @@ pub fn build_map(graph: &mut Graph, resource_manager: &ResourceManager) {
for x in left_x..right_x { for x in left_x..right_x {
build_ground_block(resource_manager, graph, x as f32, bottom_y); build_ground_block(resource_manager, graph, x as f32, bottom_y);
} }
build_mushroom_block(resource_manager, graph, 30.0, bottom_y); build_stone_block(resource_manager, graph, 30.0, bottom_y);
build_tree_block(resource_manager, graph, 35.0, bottom_y); build_tree_block(resource_manager, graph, 35.0, bottom_y);
build_stone_block(resource_manager, graph, -30.0, bottom_y); build_stone_block(resource_manager, graph, -30.0, bottom_y);
build_tree_block(resource_manager, graph, -35.0, bottom_y); build_tree_block(resource_manager, graph, -35.0, bottom_y);