2020-08-18 16:46:07 +00:00
|
|
|
use num_traits::clamp;
|
|
|
|
|
|
|
|
use crate::player::Player;
|
2020-08-19 13:11:34 +00:00
|
|
|
use crate::stage::Stage;
|
|
|
|
use crate::SharedGameState;
|
2020-08-20 18:31:47 +00:00
|
|
|
use crate::caret::CaretType;
|
|
|
|
use crate::common::Direction;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
const OFF_X: &[isize; 4] = &[0, 1, 0, 1];
|
|
|
|
const OFF_Y: &[isize; 4] = &[0, 0, 1, 1];
|
|
|
|
|
|
|
|
impl Player {
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_block(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
// left wall
|
|
|
|
if (self.y - self.hit.top as isize) < (y * 0x10 + 4) * 0x200
|
|
|
|
&& self.y + self.hit.bottom as isize > (y * 0x10 - 4) * 0x200
|
|
|
|
&& (self.x - self.hit.right as isize) < (x * 0x10 + 8) * 0x200
|
|
|
|
&& (self.x - self.hit.right as isize) > x * 0x10 * 0x200 {
|
|
|
|
self.x = ((x * 0x10 + 8) * 0x200) + self.hit.right as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_x < -0x180 {
|
|
|
|
self.vel_x = -0x180;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if !state.key_state.left() && self.vel_x < 0 {
|
|
|
|
self.vel_x = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x01(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// right wall
|
|
|
|
if (self.y - self.hit.top as isize) < (y * 0x10 + 4) * 0x200
|
|
|
|
&& self.y + self.hit.bottom as isize > (y * 0x10 - 4) * 0x200
|
|
|
|
&& (self.x + self.hit.right as isize) > (x * 0x10 - 8) * 0x200
|
|
|
|
&& (self.x + self.hit.right as isize) < x * 0x10 * 0x200 {
|
|
|
|
self.x = ((x * 0x10 - 8) * 0x200) - self.hit.right as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_x > 0x180 {
|
|
|
|
self.vel_x = 0x180;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if !state.key_state.right() && self.vel_x > 0 {
|
|
|
|
self.vel_x = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x04(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ceiling
|
|
|
|
if (self.x - self.hit.right as isize) < (x * 0x10 + 5) * 0x200
|
|
|
|
&& (self.x + self.hit.right as isize) > (x * 0x10 - 5) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 8) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) > y * 0x10 * 0x200 {
|
|
|
|
self.y = ((y * 0x10 + 8) * 0x200) + self.hit.top as isize;
|
|
|
|
|
2020-08-28 02:12:13 +00:00
|
|
|
if !self.cond.hidden() && self.vel_y < -0x200 {
|
2020-08-21 05:27:26 +00:00
|
|
|
self.flags.set_head_bounced(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y < 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x02(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// floor
|
|
|
|
if ((self.x - self.hit.right as isize) < (x * 0x10 + 5) * 0x200)
|
|
|
|
&& ((self.x + self.hit.right as isize) > (x * 0x10 - 5) * 0x200)
|
|
|
|
&& ((self.y + self.hit.bottom as isize) > (y * 0x10 - 8) * 0x200)
|
|
|
|
&& ((self.y + self.hit.bottom as isize) < y * 0x10 * 0x200) {
|
|
|
|
self.y = ((y * 0x10 - 8) * 0x200) - self.hit.bottom as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0x400 {
|
2020-08-18 16:46:07 +00:00
|
|
|
// PlaySoundObject(23, SOUND_MODE_PLAY); todo
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x08(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_a(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
if self.x < (x * 0x10 + 8) * 0x200
|
|
|
|
&& self.x > (x * 0x10 - 8) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 * 0x200) - (self.x - x * 0x10 * 0x200) / 2 + 0x800
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 - 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) - ((self.x - x * 0x10 * 0x200) / 2) + 0x800 + self.hit.top as isize;
|
|
|
|
|
2020-08-28 02:12:13 +00:00
|
|
|
if !self.cond.hidden() && self.vel_y < -0x200 {
|
2020-08-21 05:27:26 +00:00
|
|
|
self.flags.set_head_bounced(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y < 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x02(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_b(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
if self.x < (x * 0x10 + 8) * 0x200
|
|
|
|
&& self.x > (x * 0x10 - 8) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 * 0x200) - (self.x - x * 0x10 * 0x200) / 2 - 0x800
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 - 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) - ((self.x - x * 0x10 * 0x200) / 2) - 0x800 + self.hit.top as isize;
|
|
|
|
|
2020-08-28 02:12:13 +00:00
|
|
|
if !self.cond.hidden() && self.vel_y < -0x200 {
|
2020-08-21 05:27:26 +00:00
|
|
|
self.flags.set_head_bounced(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y < 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x02(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_c(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
if self.x < (x * 0x10 + 8) * 0x200
|
|
|
|
&& self.x > (x * 0x10 - 8) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 * 0x200) + (self.x - x * 0x10 * 0x200) / 2 - 0x800
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 - 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) + ((self.x - x * 0x10 * 0x200) / 2) - 0x800 + self.hit.top as isize;
|
|
|
|
|
2020-08-28 02:12:13 +00:00
|
|
|
if !self.cond.hidden() && self.vel_y < -0x200 {
|
2020-08-21 05:27:26 +00:00
|
|
|
self.flags.set_head_bounced(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y < 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x02(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_d(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
if (self.x < (x * 0x10 + 8) * 0x200)
|
|
|
|
&& (self.x > (x * 0x10 - 8) * 0x200)
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 * 0x200) + (self.x - x * 0x10 * 0x200) / 2 + 0x800
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 - 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) + ((self.x - x * 0x10 * 0x200) / 2) + 0x800 + self.hit.top as isize;
|
|
|
|
|
2020-08-28 02:12:13 +00:00
|
|
|
if !self.cond.hidden() && self.vel_y < -0x200 {
|
2020-08-21 05:27:26 +00:00
|
|
|
self.flags.set_head_bounced(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y < 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x02(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_e(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
self.flags.set_flag_x10000(true);
|
|
|
|
|
|
|
|
if (self.x < (x * 0x10 + 8) * 0x200)
|
|
|
|
&& (self.x > (x * 0x10 - 8) * 0x200)
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 * 0x200) + (self.x - x * 0x10 * 0x200) / 2 - 0x800
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) + ((self.x - x * 0x10 * 0x200) / 2) - 0x800 - self.hit.bottom as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0x400 {
|
2020-08-18 16:46:07 +00:00
|
|
|
// PlaySoundObject(23, SOUND_MODE_PLAY); todo
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x20(true);
|
|
|
|
self.flags.set_flag_x08(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_f(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
self.flags.set_flag_x20000(true);
|
|
|
|
|
|
|
|
if (self.x < (x * 0x10 + 8) * 0x200)
|
|
|
|
&& (self.x > (x * 0x10 - 8) * 0x200)
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 * 0x200) + (self.x - x * 0x10 * 0x200) / 2 + 0x800
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) + ((self.x - x * 0x10 * 0x200) / 2) + 0x800 - self.hit.bottom as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0x400 {
|
2020-08-18 16:46:07 +00:00
|
|
|
// PlaySoundObject(23, SOUND_MODE_PLAY); todo
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x20(true);
|
|
|
|
self.flags.set_flag_x08(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_g(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
self.flags.set_flag_x40000(true);
|
|
|
|
|
|
|
|
if (self.x < (x * 0x10 + 8) * 0x200)
|
|
|
|
&& (self.x > (x * 0x10 - 8) * 0x200)
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 * 0x200) - (self.x - x * 0x10 * 0x200) / 2 + 0x800
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) - ((self.x - x * 0x10 * 0x200) / 2) + 0x800 - self.hit.bottom as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0x400 {
|
2020-08-18 16:46:07 +00:00
|
|
|
// PlaySoundObject(23, SOUND_MODE_PLAY); todo
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x10(true);
|
|
|
|
self.flags.set_flag_x08(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_triangle_h(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
self.flags.set_flag_x80000(true);
|
|
|
|
|
|
|
|
if (self.x < (x * 0x10 + 8) * 0x200)
|
|
|
|
&& (self.x > (x * 0x10 - 8) * 0x200)
|
|
|
|
&& (self.y + self.hit.bottom as isize) > (y * 0x10 * 0x200) - (self.x - x * 0x10 * 0x200) / 2 - 0x800
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 8) * 0x200 {
|
|
|
|
self.y = (y * 0x10 * 0x200) - ((self.x - x * 0x10 * 0x200) / 2) - 0x800 - self.hit.bottom as isize;
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0x400 {
|
2020-08-18 16:46:07 +00:00
|
|
|
// PlaySoundObject(23, SOUND_MODE_PLAY); todo
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:31:47 +00:00
|
|
|
if self.vel_y > 0 {
|
|
|
|
self.vel_y = 0;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flags.set_flag_x10(true);
|
|
|
|
self.flags.set_flag_x08(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn judge_hit_water(&mut self, state: &SharedGameState, x: isize, y: isize) {
|
2020-08-18 16:46:07 +00:00
|
|
|
if (self.x - self.hit.right as isize) < (x * 0x10 + 5) * 0x200
|
|
|
|
&& (self.x + self.hit.right as isize) > (x * 0x10 - 5) * 0x200
|
|
|
|
&& (self.y - self.hit.top as isize) < (y * 0x10 + 5) * 0x200
|
|
|
|
&& (self.y + self.hit.bottom as isize) > y * 0x10 * 0x200 {
|
|
|
|
self.flags.set_underwater(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
pub fn tick_map_collisions(&mut self, state: &SharedGameState, stage: &Stage) {
|
|
|
|
let x = clamp(self.x / 0x10 / 0x200, 0, stage.map.width as isize);
|
|
|
|
let y = clamp(self.y / 0x10 / 0x200, 0, stage.map.height as isize);
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
for (ox, oy) in OFF_X.iter().zip(OFF_Y) {
|
2020-08-19 13:11:34 +00:00
|
|
|
let attrib = stage.map.get_attribute((x + *ox) as usize, (y + *oy) as usize);
|
2020-08-18 16:46:07 +00:00
|
|
|
match attrib {
|
|
|
|
// Block
|
|
|
|
0x02 | 0x60 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_water(state, x + *ox, y + *oy);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x05 | 0x41 | 0x43 | 0x46 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_block(state, x + *ox, y + *oy);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x50 | 0x70 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_a(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x51 | 0x71 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_b(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x52 | 0x72 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_c(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x53 | 0x73 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_d(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x54 | 0x74 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_e(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x55 | 0x75 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_f(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x56 | 0x76 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_g(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x57 | 0x77 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_triangle_h(state, x + *ox, y + *oy);
|
|
|
|
if attrib & 0x20 != 0 { self.judge_hit_water(state, x + *ox, y + *oy); }
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
0x61 => {
|
2020-08-19 13:11:34 +00:00
|
|
|
self.judge_hit_water(state, x + *ox, y + *oy);
|
|
|
|
self.judge_hit_block(state, x + *ox, y + *oy);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 18:31:47 +00:00
|
|
|
|
|
|
|
pub fn tick_npc_collisions(&mut self, state: &mut SharedGameState, stage: &Stage) {
|
|
|
|
if self.question {
|
|
|
|
state.create_caret(self.x, self.y, CaretType::QuestionMark, Direction::Left);
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|