Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. //main.rs
  2. extern crate piston_window;
  3. extern crate opengl_graphics;
  4. extern crate rand;
  5.  
  6. use rand::Rng;
  7. use std::rc::Rc;
  8. use std::ops::Deref;
  9. use opengl_graphics::Texture as Tex;
  10. use piston_window::*;
  11. use std::path::Path;
  12. use opengl_graphics::GlGraphics;
  13.  
  14. pub mod structs;
  15. #[derive(PartialEq, Copy, Clone, Debug)]
  16. enum ObjectType {
  17. Blocking,
  18. Passing,
  19. Crate,
  20. Target,
  21. }
  22. #[derive(PartialEq)]
  23. enum Direction {
  24. Up,
  25. Down,
  26. Left,
  27. Right,
  28. }
  29.  
  30. struct Player<'a> {
  31. sprite: &'a Tex,
  32. position: structs::Position,
  33. }
  34. struct Object<'a> {
  35. sprite: &'a Tex,
  36. obj_type: ObjectType,
  37. position: structs::Position,
  38. }
  39. struct Game<'a> {
  40. gl: GlGraphics,
  41. floor: Vec<Object<'a>>,
  42. special: Vec<Object<'a>>,
  43. player: Player<'a>,
  44. }
  45. struct PlayerTextures {
  46. player_n: Tex,
  47. player_s: Tex,
  48. player_e: Tex,
  49. player_w: Tex,
  50. }
  51. struct GameTextures {
  52. wall: Tex,
  53. floor: Tex,
  54. _crate: Tex,
  55. target: Tex,
  56. }
  57.  
  58. impl<'a> Game<'a> {
  59. fn check(&mut self, position: &structs::Position, direction: Direction) {
  60. use Direction::*;
  61.  
  62. let mut next = (*position).clone();
  63. let mut is_crt = false;
  64. let mut obj = None;
  65.  
  66. match direction {
  67. Down => next = next + structs::Position::new(0, 1).unwrap(),
  68. Up => next = next + structs::Position::new(0, -1).unwrap(),
  69. Left => next = next + structs::Position::new(-1, 0).unwrap(),
  70. Right => next = next + structs::Position::new(1, 0).unwrap(),
  71. }
  72.  
  73. {
  74. let mut iter = self.special.iter_mut();
  75. let fnd = iter
  76. .find(|&&mut ref x| x.position == next && !(x.obj_type == ObjectType::Target));
  77. if fnd.is_none() {
  78. println!("did not find obstacle for player at: {:?}", next);
  79. self.player.position = next;
  80. } else {
  81. match direction {
  82. Down => next = next + structs::Position::new(0, 1).unwrap(),
  83. Up => next = next + structs::Position::new(0, -1).unwrap(),
  84. Left => next = next + structs::Position::new(-1, 0).unwrap(),
  85. Right => next = next + structs::Position::new(1, 0).unwrap(),
  86. }
  87.  
  88. obj = fnd;
  89.  
  90. if obj.as_ref().unwrap().obj_type == ObjectType::Crate {
  91. is_crt = true;
  92. }
  93. }
  94.  
  95. }
  96. {
  97. if is_crt {
  98. let mut iter = self.special.iter_mut();
  99. let fnd = iter.find(|&&mut ref x| x.position == next);
  100. if fnd.is_none() {
  101. println!("did not find crate obstacle at: {:?}", next);
  102. obj.unwrap().position = next;
  103. } else {
  104. let mut obj2 = fnd.unwrap();
  105. if obj2.obj_type == ObjectType::Target {
  106. obj2.position = structs::Position::new(0, 0).unwrap();
  107. }
  108. }
  109. }
  110. }
  111.  
  112. }
  113.  
  114. fn render(&mut self, args: &RenderArgs) {
  115. let iter = self.floor.iter().chain(self.special.iter());
  116. let player = &self.player;
  117. self.gl.draw(args.viewport(), |c, g| {
  118.  
  119. clear([1.0, 1.0, 1.0, 1.0], g);
  120.  
  121. for img in iter {
  122. let pos = &img.position;
  123. let transform = c.transform
  124. .trans(((pos.get_x() * 64)) as f64, ((pos.get_y() * 64)) as f64);
  125. image(img.sprite, transform, g);
  126. }
  127. image(player.sprite.clone().deref(),
  128. c.transform.trans((player.position.get_x() * 64) as f64,
  129. (player.position.get_y() * 64) as f64),
  130. g);
  131. });
  132. }
  133. fn update(&mut self, args: &UpdateArgs) {}
  134. }
  135.  
  136.  
  137. fn main() {
  138.  
  139. let (width, height) = (64 * 10, 64 * 10);
  140. let opengl = OpenGL::V3_2;
  141. let mut window: PistonWindow = WindowSettings::new("piston", (width, height))
  142. .exit_on_esc(true)
  143. .opengl(opengl)
  144. .build()
  145. .unwrap();
  146. window.hide();
  147. println!("Loading...");
  148. let player_tex = PlayerTextures {
  149. player_n: Tex::from_path("./assets/player_n.png").unwrap(),
  150. player_s: Tex::from_path("./assets/player_s.png").unwrap(),
  151. player_e: Tex::from_path("./assets/player_e.png").unwrap(),
  152. player_w: Tex::from_path("./assets/player_w.png").unwrap(),
  153. };
  154. let obj_tex = GameTextures {
  155. wall: Tex::from_path("./assets/wall.png").unwrap(),
  156. floor: Tex::from_path("./assets/floor.png").unwrap(),
  157. _crate: Tex::from_path("./assets/crates/crate_19.png").unwrap(),
  158. target: Tex::from_path("./assets/crates/crate_29.png").unwrap(),
  159. };
  160. let mut player = Player {
  161. sprite: &player_tex.player_n,
  162. position: structs::Position::new(3, 3).unwrap(),
  163. };
  164.  
  165. let mut game = Game {
  166. gl: GlGraphics::new(opengl),
  167. floor: Vec::new(),
  168. special: Vec::new(),
  169. player: player,
  170. };
  171. let mut rand = rand::thread_rng();
  172. let mut crates = Vec::new();
  173.  
  174. for i in 0..10 {
  175. for j in 0..10 {
  176. if i == 0 || i == 9 || j == 0 || j == 9 {
  177. let obj = Object {
  178. sprite: &obj_tex.wall,
  179. obj_type: ObjectType::Blocking,
  180. position: structs::Position::new(i, j).unwrap(),
  181. };
  182. game.special.push(obj);
  183.  
  184. } else {
  185. let r: f32 = rand.gen_range(0., 1.);
  186. let obj = Object {
  187. sprite: &obj_tex.floor,
  188. obj_type: ObjectType::Passing,
  189. position: structs::Position::new(i, j).unwrap(),
  190. };
  191. game.floor.push(obj);
  192.  
  193. if i > 1 && i < 8 && j > 1 && j < 8 && r <= 0.12 {
  194. crates.push(Object {
  195. sprite: &obj_tex._crate,
  196. obj_type: ObjectType::Crate,
  197. position: structs::Position::new(i, j).unwrap(),
  198. });
  199. }
  200.  
  201.  
  202. }
  203. }
  204. }
  205.  
  206. let mut targets = 0;
  207. let crates_ = crates.len();
  208. let mut target = Vec::with_capacity(crates_);
  209. for i in 1..9 {
  210. for j in 1..9 {
  211. if !(i == 0 || i == 9 || j == 0 || j == 9) {
  212. let r: f32 = rand.gen_range(0., 1.);
  213. if targets < crates_ && r > 0.85 {
  214.  
  215. target.push(Object {
  216. sprite: &obj_tex.target,
  217. obj_type: ObjectType::Target,
  218. position: structs::Position::new(i, j).unwrap(),
  219. });
  220. targets += 1;
  221. }
  222.  
  223.  
  224. }
  225. }
  226. }
  227. target.append(&mut crates);
  228. game.special.append(&mut target);
  229. window.show();
  230. while let Some(e) = window.next() {
  231.  
  232. if let Some(Button::Keyboard(key)) = e.press_args() {
  233. let mut pos = game.player.position.clone();
  234. let mut spr = game.player.sprite;
  235.  
  236. match key {
  237. Key::Up => {
  238. game.check(&pos, Direction::Up);
  239. spr = &player_tex.player_n;
  240. }
  241. Key::Down => {
  242. game.check(&pos, Direction::Down);
  243. spr = &player_tex.player_s;
  244. }
  245. Key::Left => {
  246. game.check(&pos, Direction::Left);
  247. spr = &player_tex.player_w;
  248. }
  249. Key::Right => {
  250. game.check(&pos, Direction::Right);
  251. spr = &player_tex.player_e;
  252. }
  253. _ => (),
  254. }
  255. game.player.sprite = spr;
  256. }
  257.  
  258. if let Some(r) = e.render_args() {
  259. game.render(&r);
  260. }
  261. if let Some(u) = e.update_args() {
  262. game.update(&u);
  263. }
  264. }
  265.  
  266. }
  267. //structs.rs
  268.  
  269. use std::ops::Add;
  270.  
  271. #[derive(Debug,PartialEq,Clone, Copy)]
  272. pub struct Position {
  273. x: i32,
  274. y: i32,
  275. }
  276.  
  277.  
  278. impl Add for Position {
  279. type Output = Position;
  280.  
  281. fn add(self, other: Position) -> Position {
  282. Position {
  283. x: self.x + other.x,
  284. y: self.y + other.y,
  285. }
  286. }
  287. }
  288.  
  289. impl Position {
  290. pub fn new(x: i32, y: i32) -> Option<Position> {
  291. if x > -2 && y > -2 && x < 10 && y < 10 {
  292. Some(Position { x: x, y: y })
  293. } else {
  294. println!("barraca");
  295. None
  296. }
  297. }
  298.  
  299. pub fn get_x(&self) -> i32 {
  300. self.x
  301. }
  302.  
  303. pub fn get_y(&self) -> i32 {
  304. self.y
  305. }
  306.  
  307. pub fn add_x(&mut self, x: i32) {
  308. self.x += x;
  309. }
  310.  
  311. pub fn add_y(&mut self, y: i32) {
  312. self.y += y;
  313. }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement