Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- extern crate piston;
- extern crate graphics;
- extern crate glutin_window;
- extern crate opengl_graphics;
- use piston::window::WindowSettings;
- use piston::event_loop::*;
- use piston::input::*;
- use glutin_window::GlutinWindow as Window;
- use opengl_graphics::{ GlGraphics, OpenGL };
- #[derive(Clone, Copy)]
- struct Position{
- x: i32,
- y: i32
- }
- impl Position{
- fn new(_x: i32, _y: i32) -> Position{
- Position{x: _x, y: _y}
- }
- }
- trait Object{
- fn logic(&mut self);
- fn draw(&self);
- fn get_name(&self)->&String;
- fn get_position(&self)->Position;
- fn set_position(&mut self, _position: Position);
- }
- struct Level{
- map: [[Box<dyn Object>; 64]; 64] // That mean that variable map can contains only structs that have Logic and Draw functions
- }
- impl Level{
- fn set_object_to(&mut self, object: Box<dyn Object>){
- let position = object.get_position();
- println!("{} placed on {}, {} position", object.get_name(), position.x, position.y);
- self.map[position.x as usize][position.y as usize] = object;
- }
- }
- #[derive(Clone)]
- struct Player{
- name: String,
- position: Position
- }
- impl Player{
- fn new(_name: String, _position: Position) -> Player{
- Player {name: _name, position: _position}
- }
- }
- impl Object for Player{
- fn logic(&mut self){
- }
- fn draw(&self){
- }
- fn get_name(&self)->&String{
- &self.name
- }
- fn get_position(&self)-> Position{
- self.position.clone()
- }
- fn set_position(&mut self, _position: Position){
- self.position = _position;
- }
- }
- #[derive(Clone)]
- struct Item{
- name: String,
- position: Position
- }
- impl Item{
- fn new(_name: String,_position: Position) -> Item{
- Item {name: _name, position: _position}
- }
- }
- impl Object for Item{
- fn logic(&mut self){
- }
- fn draw(&self){
- }
- fn get_name(&self)->&String{
- &self.name
- }
- fn get_position(&self)-> Position{
- self.position.clone()
- }
- fn set_position(&mut self, _position: Position){
- self.position = _position;
- }
- }
- #[derive(Clone, Copy)]
- struct Block<'a>{
- name: &'a String,
- position: Position
- }
- impl<'a> Block<'a>{
- fn new(_name: &'a String,_position: Position) -> Block<'a>{
- Block {name: _name, position: _position}
- }
- }
- impl<'a> Object for Block<'a>{
- fn logic(&mut self){
- }
- fn draw(&self){
- }
- fn get_name(&self)->&String{
- &self.name
- }
- fn get_position(&self)-> Position{
- self.position.clone()
- }
- fn set_position(&mut self, _position: Position){
- self.position = _position;
- }
- }
- fn main() {
- let opengl = OpenGL::V3_2;
- // Create an Glutin window.
- let mut window: Window = WindowSettings::new(
- "spinning-square",
- [200, 200]
- )
- .opengl(opengl)
- .exit_on_esc(true)
- .build()
- .unwrap();
- // Create a new game and run it.
- //
- //let player = Box::new(Player::new("Jhon".to_string(), Position::new(10, 10)));
- let mut level: Level = Level{map: [[Box::new(Block::new(&"f".to_string(), Position::new(1,1))); 64]; 64]};
- level.set_object_to(Box::new(Player::new("Jhon".to_string(), Position::new(10, 10))));
- let mut events = Events::new(EventSettings::new());
- while let Some(e) = events.next(&mut window) {
- if let Some(r) = e.render_args() {
- //
- }
- if let Some(u) = e.update_args() {
- //
- }
- }
- }
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.

