1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-17 11:22:52 +00:00
doukutsu-rs/src/npc/ai/first_cave.rs

226 lines
6.7 KiB
Rust
Raw Normal View History

use num_traits::clamp;
2020-09-09 13:28:58 +00:00
use crate::common::Direction;
2022-03-26 09:21:08 +00:00
use crate::framework::error::GameResult;
2020-09-06 00:37:42 +00:00
use crate::npc::NPC;
use crate::player::Player;
use crate::rng::RNG;
use crate::shared_game_state::SharedGameState;
2020-09-06 00:37:42 +00:00
impl NPC {
pub(crate) fn tick_n059_eye_door(&mut self, state: &mut SharedGameState, players: [&mut Player; 2]) -> GameResult {
2020-09-06 00:37:42 +00:00
match self.action_num {
0 | 1 => {
if self.action_num == 0 {
self.action_num = 1;
}
let player = self.get_closest_player_mut(players);
2022-03-26 09:21:08 +00:00
if self.x - 0x8000 < player.x
&& self.x + 0x8000 > player.x
&& self.y - 0x8000 < player.y
&& self.y + 0x8000 > player.y
{
2020-09-13 00:10:49 +00:00
self.action_num = 2;
self.anim_counter = 0;
}
}
2 => {
self.anim_counter += 1;
if self.anim_counter > 2 {
self.anim_counter = 0;
self.anim_num += 1;
if self.anim_num == 2 {
self.action_num = 3;
}
}
}
3 => {
let player = self.get_closest_player_mut(players);
2022-03-26 09:21:08 +00:00
if !(self.x - 0x8000 < player.x
&& self.x + 0x8000 > player.x
&& self.y - 0x8000 < player.y
&& self.y + 0x8000 > player.y)
{
2020-09-13 00:10:49 +00:00
self.action_num = 4;
self.anim_counter = 0;
}
}
4 => {
self.anim_counter += 1;
if self.anim_counter > 2 {
self.anim_counter = 0;
self.anim_num -= 1;
if self.anim_num == 0 {
self.action_num = 1;
}
}
2020-09-06 00:37:42 +00:00
}
_ => (),
2020-09-06 00:37:42 +00:00
}
2020-09-13 00:10:49 +00:00
if self.shock > 0 {
self.anim_rect = state.constants.npc.n059_eye_door[3];
} else {
self.anim_rect = state.constants.npc.n059_eye_door[self.anim_num as usize];
}
2020-09-06 00:37:42 +00:00
Ok(())
}
2022-03-26 09:21:08 +00:00
pub(crate) fn tick_n064_first_cave_critter(
&mut self,
state: &mut SharedGameState,
players: [&mut Player; 2],
) -> GameResult {
2020-09-09 13:28:58 +00:00
match self.action_num {
0 | 1 => {
if self.action_num == 0 {
2021-05-05 16:34:14 +00:00
self.y += 0x600;
2020-09-09 13:28:58 +00:00
self.action_num = 1;
self.anim_num = 0;
}
let player = self.get_closest_player_mut(players);
2022-03-26 09:21:08 +00:00
self.face_player(player);
2020-09-09 13:28:58 +00:00
if self.target_x < 100 {
self.target_x += 1;
}
if self.action_counter >= 8
2022-03-26 09:21:08 +00:00
&& self.x - 0xe000 < player.x
&& self.x + 0xe000 > player.x
&& self.y - 0xa000 < player.y
&& self.y + 0xa000 > player.y
{
self.anim_num = 1;
2020-09-09 13:28:58 +00:00
} else {
if self.action_counter < 8 {
self.action_counter += 1;
}
2022-03-26 09:21:08 +00:00
self.anim_num = 0;
2020-09-09 13:28:58 +00:00
}
if self.shock > 0 {
self.action_num = 2;
self.action_counter = 0;
2022-03-26 09:21:08 +00:00
self.anim_num = 0;
2020-09-09 13:28:58 +00:00
}
if self.action_counter >= 8
&& self.target_x >= 100
2022-03-26 09:21:08 +00:00
&& self.x - 0x8000 < player.x
&& self.x + 0x8000 > player.x
&& self.y - 0xa000 < player.y
&& self.y + 0xa000 > player.y
{
2020-09-09 13:28:58 +00:00
self.action_num = 2;
self.action_counter = 0;
2022-03-26 09:21:08 +00:00
self.anim_num = 0;
2020-09-09 13:28:58 +00:00
}
}
2 => {
self.action_counter += 1;
if self.action_counter > 8 {
self.action_num = 3;
2022-03-26 09:21:08 +00:00
self.anim_num = 2;
2020-09-09 13:28:58 +00:00
2022-03-26 09:21:08 +00:00
self.vel_x = self.direction.vector_x() * 0x100;
2020-09-09 13:28:58 +00:00
self.vel_y = -0x5ff;
2022-03-26 09:21:08 +00:00
state.sound_manager.play_sfx(30);
2020-09-09 13:28:58 +00:00
}
}
3 => {
if self.flags.hit_bottom_wall() {
self.vel_x = 0;
2022-03-26 09:21:08 +00:00
self.anim_num = 0;
2020-09-09 13:28:58 +00:00
self.action_counter = 0;
self.action_num = 1;
2020-09-16 19:53:53 +00:00
state.sound_manager.play_sfx(23);
2020-09-09 13:28:58 +00:00
}
}
_ => (),
2020-09-09 13:28:58 +00:00
}
self.vel_y += 0x40;
if self.vel_y > 0x5ff {
self.vel_y = 0x5ff;
}
self.x += self.vel_x;
self.y += self.vel_y;
2022-03-26 09:21:08 +00:00
let dir_offset = if self.direction == Direction::Left { 0 } else { 3 };
self.anim_rect = state.constants.npc.n064_first_cave_critter[self.anim_num as usize + dir_offset];
2020-09-09 13:28:58 +00:00
Ok(())
}
2020-09-09 16:25:39 +00:00
2022-03-26 09:21:08 +00:00
pub(crate) fn tick_n065_first_cave_bat(
&mut self,
state: &mut SharedGameState,
players: [&mut Player; 2],
) -> GameResult {
2020-09-09 16:25:39 +00:00
match self.action_num {
0 | 1 => {
if self.action_num == 0 {
self.target_x = self.x;
self.target_y = self.y;
self.action_num = 1;
self.action_counter = self.rng.range(0..50) as u16;
2020-09-09 16:25:39 +00:00
}
self.action_counter += 1;
if self.action_counter > 50 {
self.action_num = 2;
self.action_counter = 0;
self.vel_y = 0x300;
}
}
2 => {
let player = self.get_closest_player_mut(players);
2020-09-09 16:25:39 +00:00
if self.x > player.x {
self.direction = Direction::Left;
} else {
self.direction = Direction::Right;
}
if self.target_y < self.y {
self.vel_y -= 0x10;
} else if self.target_y > self.y {
self.vel_y += 0x10;
}
self.vel_y = clamp(self.vel_y, -0x300, 0x300);
}
_ => (),
2020-09-09 16:25:39 +00:00
}
self.x += self.vel_x;
self.y += self.vel_y;
self.anim_counter += 1;
if self.anim_counter > 1 {
self.anim_counter = 0;
self.anim_num += 1;
if self.anim_num > 2 {
self.anim_num = 0;
}
2022-03-26 09:21:08 +00:00
self.anim_rect = state.constants.npc.n065_first_cave_bat
[self.anim_num as usize + if self.direction == Direction::Right { 4 } else { 0 }];
2020-09-09 16:25:39 +00:00
}
Ok(())
}
2020-09-06 00:37:42 +00:00
}