use ggez::timer::delta;
use ggez::{Context, GameResult};
-const WALK_TIME: f32 = 0.4f32;
+const WALK_TIME: f32 = 0.34f32;
pub enum PlayerState {
Standing,
pub y: f32,
state: PlayerState,
pub color: Color,
+ xflip: bool,
}
impl Player {
pub fn new(ctx: &mut Context, color: Color) -> GameResult<Self> {
Ok(Self {
sprite: Image::new(ctx, "/player_sprite.png")?,
- x: 0f32,
- y: 0f32,
+ x: 300f32,
+ y: 300f32,
state: PlayerState::Standing,
color,
+ xflip: false,
})
}
self.state = PlayerState::Walking(true, 0f32);
}
}
+
+ pub fn set_xflip(&mut self, xflip: bool) {
+ self.xflip = xflip;
+ }
}
impl EventHandler for Player {
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
match &self.state {
PlayerState::Standing => {
- graphics::draw(
- ctx,
- &self.sprite,
- DrawParam::new()
- .src(Rect::new(0f32, 0f32, 0.3333333333333f32, 1f32))
- .dest([self.x, self.y])
- .color(self.color),
- )?;
- }
- PlayerState::Walking(left, _) => {
- if *left {
+ if self.xflip {
graphics::draw(
ctx,
&self.sprite,
DrawParam::new()
- .src(Rect::new(
- 0.3333333333333f32,
- 0f32,
- 0.3333333333333f32,
- 1f32,
- ))
+ .src(Rect::new(0f32, 0f32, 0.3333333333333f32, 1f32))
.dest([self.x, self.y])
- .color(self.color),
+ .color(self.color)
+ .scale([-1f32, 1f32])
+ .offset([1f32, 0f32]),
)?;
} else {
graphics::draw(
ctx,
&self.sprite,
DrawParam::new()
- .src(Rect::new(
- 0.6666666666666f32,
- 0f32,
- 0.3333333333333f32,
- 1f32,
- ))
+ .src(Rect::new(0f32, 0f32, 0.3333333333333f32, 1f32))
.dest([self.x, self.y])
.color(self.color),
)?;
}
}
+ PlayerState::Walking(left, _) => {
+ if *left {
+ if self.xflip {
+ graphics::draw(
+ ctx,
+ &self.sprite,
+ DrawParam::new()
+ .src(Rect::new(
+ 0.3333333333333f32,
+ 0f32,
+ 0.3333333333333f32,
+ 1f32,
+ ))
+ .dest([self.x, self.y])
+ .color(self.color)
+ .scale([-1f32, 1f32])
+ .offset([1f32, 0f32]),
+ )?;
+ } else {
+ graphics::draw(
+ ctx,
+ &self.sprite,
+ DrawParam::new()
+ .src(Rect::new(
+ 0.3333333333333f32,
+ 0f32,
+ 0.3333333333333f32,
+ 1f32,
+ ))
+ .dest([self.x, self.y])
+ .color(self.color),
+ )?;
+ }
+ } else {
+ if self.xflip {
+ graphics::draw(
+ ctx,
+ &self.sprite,
+ DrawParam::new()
+ .src(Rect::new(
+ 0.6666666666666f32,
+ 0f32,
+ 0.3333333333333f32,
+ 1f32,
+ ))
+ .dest([self.x, self.y])
+ .color(self.color)
+ .scale([-1f32, 1f32])
+ .offset([1f32, 0f32]),
+ )?;
+ } else {
+ graphics::draw(
+ ctx,
+ &self.sprite,
+ DrawParam::new()
+ .src(Rect::new(
+ 0.6666666666666f32,
+ 0f32,
+ 0.3333333333333f32,
+ 1f32,
+ ))
+ .dest([self.x, self.y])
+ .color(self.color),
+ )?;
+ }
+ }
+ }
}
Ok(())
}
use std::cell::RefCell;
use std::rc::Rc;
+use ggez::audio::{SoundSource, Source};
use ggez::event::EventHandler;
-use ggez::graphics::{self, Font, Text};
+use ggez::graphics::{self, DrawParam, Font, Image, Text};
use ggez::input::mouse::MouseButton;
use ggez::{Context, GameResult};
current_text: Text,
final_text: String,
text_idx: usize,
+ music: Source,
+ pod_image: Image,
+ pod_empty_image: Image,
}
impl MainScene {
- pub fn new(_ctx: &mut Context, font: Font, player: Rc<RefCell<Player>>) -> Self {
+ pub fn new(ctx: &mut Context, font: Font, player: Rc<RefCell<Player>>) -> Self {
+ let mut music = Source::new(ctx, "/music00.ogg").unwrap();
+ music.set_repeat(true);
+ // music.play().unwrap();
Self {
font,
player,
current_text: Text::new("".to_owned()),
final_text: String::new(),
text_idx: 0usize,
+ music,
+ pod_image: Image::new(ctx, "/stasis_pod.png").unwrap(),
+ pod_empty_image: Image::new(ctx, "/stasis_pod_empty.png").unwrap(),
}
}
}
impl EventHandler for MainScene {
- fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
+ fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
+ self.player.borrow_mut().update(ctx)?;
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
self.player.borrow_mut().draw(ctx)?;
+ graphics::draw(
+ ctx,
+ &self.pod_image,
+ DrawParam::new().dest([600f32, 170f32]).rotation(0.7f32),
+ )?;
Ok(())
}