.mouse_button_down_event(ctx, button, x, y);
}
+ fn mouse_button_up_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
+ self.current_scene.mouse_button_up_event(ctx, button, x, y);
+ }
+
fn key_down_event(
&mut self,
ctx: &mut Context,
self.current_scene
.key_down_event(ctx, keycode, keymods, repeat);
}
+
+ fn key_up_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods) {
+ self.current_scene.key_up_event(ctx, keycode, keymods);
+ }
}
const WALK_TIME: f32 = 0.34f32;
+#[derive(PartialEq)]
pub enum PlayerState {
Standing,
Walking(bool, f32),
pub fn set_walking(&mut self, is_walking: bool) {
if is_walking {
- self.state = PlayerState::Standing;
+ if self.state == PlayerState::Standing {
+ self.state = PlayerState::Walking(true, 0f32);
+ }
} else {
- self.state = PlayerState::Walking(true, 0f32);
+ self.state = PlayerState::Standing;
}
}
let dt = delta(ctx);
match &mut self.state {
PlayerState::Standing => (),
- PlayerState::Walking(left, timer) => {
+ PlayerState::Walking(ref mut left, ref mut timer) => {
*timer += dt.as_secs_f32();
if *timer >= WALK_TIME {
*timer -= WALK_TIME;
*left = !*left;
}
+ //println!("{}", timer);
}
}
Ok(())
)?;
}
}
- PlayerState::Walking(left, _) => {
- if *left {
+ PlayerState::Walking(step, _) => {
+ if *step {
+ //print!("step 0, ");
if self.xflip {
graphics::draw(
ctx,
.scale([-1f32, 1f32])
.offset([1f32, 0f32]),
)?;
+ //println!("left");
} else {
graphics::draw(
ctx,
.rotation(self.rot)
.color(self.color),
)?;
+ //println!("right");
}
} else {
+ //print!("step 1, ");
if self.xflip {
graphics::draw(
ctx,
.scale([-1f32, 1f32])
.offset([1f32, 0f32]),
)?;
+ //println!("left");
} else {
graphics::draw(
ctx,
.rotation(self.rot)
.color(self.color),
)?;
+ //println!("right");
}
}
}
const TEXT_FAST_RATE: f32 = 0.1f32;
const IN_POD_TEXT_WAIT_TIME: f32 = 1f32;
const GET_OUT_OF_POD_TIME: f32 = 3f32;
+const PLAYER_MOVEMENT_SPEED: f32 = 200f32;
enum State {
InPod_InDarkness,
StasisPod,
}
+enum WalkingState {
+ Standing,
+ Left,
+ Right,
+}
+
pub struct MainScene {
font: Font,
player: Rc<RefCell<Player>>,
draw_flicker_pod: bool,
index: usize,
room: Room,
+ walking_state: WalkingState,
}
impl MainScene {
draw_flicker_pod: false,
index: 0usize,
room: Room::StasisPod,
+ walking_state: WalkingState::Standing,
}
}
}
}
State::Investigate => {
- // TODO
+ let mut player = self.player.borrow_mut();
+ match self.walking_state {
+ WalkingState::Standing => {
+ player.set_walking(false);
+ }
+ WalkingState::Left => {
+ player.x -= dt * PLAYER_MOVEMENT_SPEED;
+ if player.x <= 0f32 {
+ player.x = 0f32;
+ self.walking_state = WalkingState::Standing;
+ player.set_walking(false);
+ } else {
+ player.set_walking(true);
+ player.set_xflip(true);
+ }
+ }
+ WalkingState::Right => {
+ player.x += dt * PLAYER_MOVEMENT_SPEED;
+ if player.x + 64f32 >= 800f32 {
+ player.x = 800f32 - 64f32;
+ self.walking_state = WalkingState::Standing;
+ player.set_walking(false);
+ } else {
+ player.set_walking(true);
+ player.set_xflip(false);
+ }
+ }
+ }
}
}
self.player.borrow_mut().update(ctx)?;
Ok(())
}
- fn mouse_button_down_event(
- &mut self,
- _ctx: &mut Context,
- _button: MouseButton,
- x: f32,
- y: f32,
- ) {
+ fn mouse_button_down_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
match self.state {
State::InPod_InDarkness => (),
State::InPod_WakeupText => {
}
}
State::GetOutOfPod => (),
- State::Investigate => {}
+ State::Investigate => {
+ if button == MouseButton::Left {
+ let player = self.player.borrow();
+ if player.x > x {
+ self.walking_state = WalkingState::Left;
+ } else if player.x + 64f32 < x {
+ self.walking_state = WalkingState::Right;
+ }
+ }
+ }
+ }
+ }
+
+ fn mouse_button_up_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
+ match self.state {
+ State::InPod_InDarkness => (),
+ State::InPod_WakeupText => (),
+ State::GetOutOfPod => (),
+ State::Investigate => {
+ if button == MouseButton::Left {
+ self.walking_state = WalkingState::Standing;
+ }
+ }
}
}
fn key_down_event(
&mut self,
- _ctx: &mut Context,
- _keycode: KeyCode,
+ ctx: &mut Context,
+ keycode: KeyCode,
_keymods: KeyMods,
_repeat: bool,
) {
}
}
State::GetOutOfPod => (),
- State::Investigate => {}
+ State::Investigate => {
+ if keycode == KeyCode::A || keycode == KeyCode::Left {
+ if self.player.borrow().x > 0f32 {
+ self.walking_state = WalkingState::Left;
+ }
+ } else if keycode == KeyCode::D || keycode == KeyCode::Right {
+ if self.player.borrow().x + 64f32 < 800f32 {
+ self.walking_state = WalkingState::Right;
+ }
+ }
+ }
+ }
+ }
+
+ fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, _keymods: KeyMods) {
+ match self.state {
+ State::InPod_InDarkness => (),
+ State::InPod_WakeupText => (),
+ State::GetOutOfPod => (),
+ State::Investigate => {
+ if keycode == KeyCode::A || keycode == KeyCode::Left {
+ self.walking_state = WalkingState::Standing;
+ } else if keycode == KeyCode::D || keycode == KeyCode::Right {
+ self.walking_state = WalkingState::Standing;
+ }
+ }
}
}
}