Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.rs
- extern crate piston_window;
- extern crate opengl_graphics;
- extern crate rand;
- use rand::Rng;
- use std::rc::Rc;
- use std::ops::Deref;
- use opengl_graphics::Texture as Tex;
- use piston_window::*;
- use std::path::Path;
- use opengl_graphics::GlGraphics;
- pub mod structs;
- #[derive(PartialEq, Copy, Clone, Debug)]
- enum ObjectType {
- Blocking,
- Passing,
- Crate,
- Target,
- }
- #[derive(PartialEq)]
- enum Direction {
- Up,
- Down,
- Left,
- Right,
- }
- struct Player<'a> {
- sprite: &'a Tex,
- position: structs::Position,
- }
- struct Object<'a> {
- sprite: &'a Tex,
- obj_type: ObjectType,
- position: structs::Position,
- }
- struct Game<'a> {
- gl: GlGraphics,
- floor: Vec<Object<'a>>,
- special: Vec<Object<'a>>,
- player: Player<'a>,
- }
- struct PlayerTextures {
- player_n: Tex,
- player_s: Tex,
- player_e: Tex,
- player_w: Tex,
- }
- struct GameTextures {
- wall: Tex,
- floor: Tex,
- _crate: Tex,
- target: Tex,
- }
- impl<'a> Game<'a> {
- fn check(&mut self, position: &structs::Position, direction: Direction) {
- use Direction::*;
- let mut next = (*position).clone();
- let mut is_crt = false;
- let mut obj = None;
- match direction {
- Down => next = next + structs::Position::new(0, 1).unwrap(),
- Up => next = next + structs::Position::new(0, -1).unwrap(),
- Left => next = next + structs::Position::new(-1, 0).unwrap(),
- Right => next = next + structs::Position::new(1, 0).unwrap(),
- }
- {
- let mut iter = self.special.iter_mut();
- let fnd = iter
- .find(|&&mut ref x| x.position == next && !(x.obj_type == ObjectType::Target));
- if fnd.is_none() {
- println!("did not find obstacle for player at: {:?}", next);
- self.player.position = next;
- } else {
- match direction {
- Down => next = next + structs::Position::new(0, 1).unwrap(),
- Up => next = next + structs::Position::new(0, -1).unwrap(),
- Left => next = next + structs::Position::new(-1, 0).unwrap(),
- Right => next = next + structs::Position::new(1, 0).unwrap(),
- }
- obj = fnd;
- if obj.as_ref().unwrap().obj_type == ObjectType::Crate {
- is_crt = true;
- }
- }
- }
- {
- if is_crt {
- let mut iter = self.special.iter_mut();
- let fnd = iter.find(|&&mut ref x| x.position == next);
- if fnd.is_none() {
- println!("did not find crate obstacle at: {:?}", next);
- obj.unwrap().position = next;
- } else {
- let mut obj2 = fnd.unwrap();
- if obj2.obj_type == ObjectType::Target {
- obj2.position = structs::Position::new(0, 0).unwrap();
- }
- }
- }
- }
- }
- fn render(&mut self, args: &RenderArgs) {
- let iter = self.floor.iter().chain(self.special.iter());
- let player = &self.player;
- self.gl.draw(args.viewport(), |c, g| {
- clear([1.0, 1.0, 1.0, 1.0], g);
- for img in iter {
- let pos = &img.position;
- let transform = c.transform
- .trans(((pos.get_x() * 64)) as f64, ((pos.get_y() * 64)) as f64);
- image(img.sprite, transform, g);
- }
- image(player.sprite.clone().deref(),
- c.transform.trans((player.position.get_x() * 64) as f64,
- (player.position.get_y() * 64) as f64),
- g);
- });
- }
- fn update(&mut self, args: &UpdateArgs) {}
- }
- fn main() {
- let (width, height) = (64 * 10, 64 * 10);
- let opengl = OpenGL::V3_2;
- let mut window: PistonWindow = WindowSettings::new("piston", (width, height))
- .exit_on_esc(true)
- .opengl(opengl)
- .build()
- .unwrap();
- window.hide();
- println!("Loading...");
- let player_tex = PlayerTextures {
- player_n: Tex::from_path("./assets/player_n.png").unwrap(),
- player_s: Tex::from_path("./assets/player_s.png").unwrap(),
- player_e: Tex::from_path("./assets/player_e.png").unwrap(),
- player_w: Tex::from_path("./assets/player_w.png").unwrap(),
- };
- let obj_tex = GameTextures {
- wall: Tex::from_path("./assets/wall.png").unwrap(),
- floor: Tex::from_path("./assets/floor.png").unwrap(),
- _crate: Tex::from_path("./assets/crates/crate_19.png").unwrap(),
- target: Tex::from_path("./assets/crates/crate_29.png").unwrap(),
- };
- let mut player = Player {
- sprite: &player_tex.player_n,
- position: structs::Position::new(3, 3).unwrap(),
- };
- let mut game = Game {
- gl: GlGraphics::new(opengl),
- floor: Vec::new(),
- special: Vec::new(),
- player: player,
- };
- let mut rand = rand::thread_rng();
- let mut crates = Vec::new();
- for i in 0..10 {
- for j in 0..10 {
- if i == 0 || i == 9 || j == 0 || j == 9 {
- let obj = Object {
- sprite: &obj_tex.wall,
- obj_type: ObjectType::Blocking,
- position: structs::Position::new(i, j).unwrap(),
- };
- game.special.push(obj);
- } else {
- let r: f32 = rand.gen_range(0., 1.);
- let obj = Object {
- sprite: &obj_tex.floor,
- obj_type: ObjectType::Passing,
- position: structs::Position::new(i, j).unwrap(),
- };
- game.floor.push(obj);
- if i > 1 && i < 8 && j > 1 && j < 8 && r <= 0.12 {
- crates.push(Object {
- sprite: &obj_tex._crate,
- obj_type: ObjectType::Crate,
- position: structs::Position::new(i, j).unwrap(),
- });
- }
- }
- }
- }
- let mut targets = 0;
- let crates_ = crates.len();
- let mut target = Vec::with_capacity(crates_);
- for i in 1..9 {
- for j in 1..9 {
- if !(i == 0 || i == 9 || j == 0 || j == 9) {
- let r: f32 = rand.gen_range(0., 1.);
- if targets < crates_ && r > 0.85 {
- target.push(Object {
- sprite: &obj_tex.target,
- obj_type: ObjectType::Target,
- position: structs::Position::new(i, j).unwrap(),
- });
- targets += 1;
- }
- }
- }
- }
- target.append(&mut crates);
- game.special.append(&mut target);
- window.show();
- while let Some(e) = window.next() {
- if let Some(Button::Keyboard(key)) = e.press_args() {
- let mut pos = game.player.position.clone();
- let mut spr = game.player.sprite;
- match key {
- Key::Up => {
- game.check(&pos, Direction::Up);
- spr = &player_tex.player_n;
- }
- Key::Down => {
- game.check(&pos, Direction::Down);
- spr = &player_tex.player_s;
- }
- Key::Left => {
- game.check(&pos, Direction::Left);
- spr = &player_tex.player_w;
- }
- Key::Right => {
- game.check(&pos, Direction::Right);
- spr = &player_tex.player_e;
- }
- _ => (),
- }
- game.player.sprite = spr;
- }
- if let Some(r) = e.render_args() {
- game.render(&r);
- }
- if let Some(u) = e.update_args() {
- game.update(&u);
- }
- }
- }
- //structs.rs
- use std::ops::Add;
- #[derive(Debug,PartialEq,Clone, Copy)]
- pub struct Position {
- x: i32,
- y: i32,
- }
- impl Add for Position {
- type Output = Position;
- fn add(self, other: Position) -> Position {
- Position {
- x: self.x + other.x,
- y: self.y + other.y,
- }
- }
- }
- impl Position {
- pub fn new(x: i32, y: i32) -> Option<Position> {
- if x > -2 && y > -2 && x < 10 && y < 10 {
- Some(Position { x: x, y: y })
- } else {
- println!("barraca");
- None
- }
- }
- pub fn get_x(&self) -> i32 {
- self.x
- }
- pub fn get_y(&self) -> i32 {
- self.y
- }
- pub fn add_x(&mut self, x: i32) {
- self.x += x;
- }
- pub fn add_y(&mut self, y: i32) {
- self.y += y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement