daily pastebin goal
64%
SHARE
TWEET

Untitled

Aleksandrk512 Jan 26th, 2019 60 in 5 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate piston;
  2. extern crate graphics;
  3. extern crate glutin_window;
  4. extern crate opengl_graphics;
  5.  
  6. use piston::window::WindowSettings;
  7. use piston::event_loop::*;
  8. use piston::input::*;
  9. use glutin_window::GlutinWindow as Window;
  10. use opengl_graphics::{ GlGraphics, OpenGL };
  11.  
  12. #[derive(Clone, Copy)]
  13. struct Position{
  14.     x: i32,
  15.     y: i32
  16. }
  17.  
  18. impl Position{
  19.     fn new(_x: i32, _y: i32) -> Position{
  20.         Position{x: _x, y: _y}
  21.     }
  22. }
  23.  
  24. trait Object{
  25.     fn logic(&mut self);
  26.     fn draw(&self);
  27.     fn get_name(&self)->&String;
  28.     fn get_position(&self)->Position;
  29.     fn set_position(&mut self, _position: Position);
  30. }
  31.  
  32. struct Level{
  33.     map:  [[Box<dyn Object>; 64]; 64] // That mean that variable map can contains only structs that have Logic and Draw functions
  34. }
  35.  
  36. impl Level{
  37.     fn set_object_to(&mut self, object: Box<dyn Object>){
  38.         let position = object.get_position();
  39.         println!("{} placed on {}, {} position", object.get_name(), position.x, position.y);
  40.         self.map[position.x as usize][position.y as usize] = object;
  41.     }
  42. }
  43.  
  44. #[derive(Clone)]
  45. struct Player{
  46.     name: String,
  47.     position: Position
  48. }
  49.  
  50. impl Player{
  51.     fn new(_name: String, _position: Position) -> Player{
  52.         Player {name: _name, position: _position}
  53.     }
  54. }
  55.  
  56. impl Object for Player{
  57.     fn logic(&mut self){
  58.  
  59.     }
  60.     fn draw(&self){
  61.  
  62.     }
  63.     fn get_name(&self)->&String{
  64.         &self.name
  65.     }
  66.     fn get_position(&self)-> Position{
  67.         self.position.clone()
  68.     }
  69.     fn set_position(&mut self, _position: Position){
  70.         self.position = _position;
  71.     }
  72. }
  73.  
  74. #[derive(Clone)]
  75. struct Item{
  76.     name: String,
  77.     position: Position
  78. }
  79.  
  80. impl Item{
  81.     fn new(_name: String,_position: Position) -> Item{
  82.         Item {name: _name, position: _position}
  83.     }
  84. }
  85.  
  86. impl Object for Item{
  87.     fn logic(&mut self){
  88.  
  89.     }
  90.     fn draw(&self){
  91.  
  92.     }
  93.     fn get_name(&self)->&String{
  94.         &self.name
  95.     }
  96.     fn get_position(&self)-> Position{
  97.         self.position.clone()
  98.     }
  99.     fn set_position(&mut self, _position: Position){
  100.         self.position = _position;
  101.     }
  102. }
  103. #[derive(Clone, Copy)]
  104. struct Block<'a>{
  105.    name: &'a String,
  106.     position: Position
  107. }
  108.  
  109. impl<'a> Block<'a>{
  110.     fn new(_name: &'a String,_position: Position) -> Block<'a>{
  111.         Block {name: _name, position: _position}
  112.     }
  113. }
  114.  
  115. impl<'a> Object for Block<'a>{
  116.     fn logic(&mut self){
  117.  
  118.     }
  119.     fn draw(&self){
  120.  
  121.     }
  122.     fn get_name(&self)->&String{
  123.         &self.name
  124.     }
  125.     fn get_position(&self)-> Position{
  126.         self.position.clone()
  127.     }
  128.     fn set_position(&mut self, _position: Position){
  129.         self.position = _position;
  130.     }
  131. }
  132.  
  133. fn main() {
  134.     let opengl = OpenGL::V3_2;
  135.     // Create an Glutin window.
  136.     let mut window: Window = WindowSettings::new(
  137.         "spinning-square",
  138.         [200, 200]
  139.     )
  140.         .opengl(opengl)
  141.         .exit_on_esc(true)
  142.         .build()
  143.         .unwrap();
  144.     // Create a new game and run it.
  145.     //
  146.     //let player = Box::new(Player::new("Jhon".to_string(), Position::new(10, 10)));
  147.     let mut level: Level = Level{map: [[Box::new(Block::new(&"f".to_string(), Position::new(1,1))); 64]; 64]};
  148.     level.set_object_to(Box::new(Player::new("Jhon".to_string(), Position::new(10, 10))));
  149.  
  150.     let mut events = Events::new(EventSettings::new());
  151.     while let Some(e) = events.next(&mut window) {
  152.         if let Some(r) = e.render_args() {
  153.             //
  154.         }
  155.  
  156.         if let Some(u) = e.update_args() {
  157.             //
  158.         }
  159.     }
  160. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top