From 32d94ef4820b37047dbf5ee14f3da3711749b380 Mon Sep 17 00:00:00 2001 From: sn99 Date: Tue, 2 Oct 2018 21:13:24 +0530 Subject: [PATCH 1/2] reformatted main.rs and edited readme --- README.md | 4 ++++ src/main.rs | 47 +++++++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b8a4bc1..02a97db 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # FirstRustGame +[![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/DeFuex/FirstRustGame/blob/master/LICENSE) +[![Crates.io](https://img.shields.io/crates/v/ggez.svg?label=ggez)](https://crates.io/crates/ggez) ## Prerequisites @@ -18,4 +20,6 @@ cargo run ## Game +Use `space` to jump and `left` `right` respectively to go left and right + screen shot 2018-07-07 at 18 03 04 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9ff1f12..479e246 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ extern crate rand; use ggez::{ContextBuilder, Context, GameResult}; use ggez::conf::{WindowMode, WindowSetup}; -use ggez::event::{ self, EventHandler, Keycode, Mod} ; -use ggez::graphics::{ Vector2 as gVector2, clear, circle, line, present, DrawMode, Point2 }; +use ggez::event::{self, EventHandler, Keycode, Mod}; +use ggez::graphics::{Vector2 as gVector2, clear, circle, line, present, DrawMode, Point2}; use ggez::timer; use rand::Rng; @@ -156,7 +156,7 @@ fn player_handle_input(actor: &mut Actor, dt: f32) { actor.direction += dt * PLAYER_TURN_RATE; //* input.xaxis; // if input.yaxis > 0.0 { - player_thrust(actor, dt); + player_thrust(actor, dt); // } } @@ -184,7 +184,6 @@ fn update_actor_position(actor: &mut Actor, dt: f32) { impl Game { fn new(_ctx: &mut Context) -> GameResult { - print_instructions(); let mut world = World::new(); @@ -195,7 +194,7 @@ impl Game { let mut player = create_player(); let walls = create_walls(5, player.pos); - + let wall_bodies = Vec::new(); // let mut player = RigidBody::new_dynamic(Ball::new(PLAYER_R), 1.0, 0.0, 0.0); @@ -203,17 +202,17 @@ impl Game { player.body.append_translation(&Translation2::new(400.0, 300.0)); let player_body = world.add_rigid_body(player.body.clone()); - let s = Game { - world, - player, - player_body, - is_walls_setup: false, - walls, - wall_bodies, - wall_collision: false, - wall_pos_col_x: 0.0, - wall_pos_col_y: 0.0, - }; + let s = Game { + world, + player, + player_body, + is_walls_setup: false, + walls, + wall_bodies, + wall_collision: false, + wall_pos_col_x: 0.0, + wall_pos_col_y: 0.0, + }; Ok(s) } @@ -259,10 +258,10 @@ impl Game { println!("player box: {}", self.player.bbox_size); println!("wall box: {}", wall.bbox_size); println!("box size: {}", self.player.bbox_size + wall.bbox_size); - println!("{}", (pdistance.norm()/1000.0) < (self.player.bbox_size + wall.bbox_size)); + println!("{}", (pdistance.norm() / 1000.0) < (self.player.bbox_size + wall.bbox_size)); /* TEST VALUES */ - if (pdistance.norm()/100.) < (self.player.bbox_size + wall.bbox_size) { + if (pdistance.norm() / 100.) < (self.player.bbox_size + wall.bbox_size) { // self.player.life = 0.0; self.wall_collision = true; self.wall_pos_col_x = wall.pos.x; @@ -314,10 +313,10 @@ impl EventHandler for Game { // let mut coords = wall_body.borrow_mut(); // let local = coords.local_to_world; line(ctx, &[Point2::new( - wall_body.borrow().position_center().x, - wall_body.borrow().position_center().y), - Point2::new(WINDOW_WIDTH as f32, - -100.0)], 1.0)?; //wall_body.borrow().position_center().coords.data + wall_body.borrow().position_center().x, + wall_body.borrow().position_center().y), + Point2::new(WINDOW_WIDTH as f32, + -100.0)], 1.0)?; //wall_body.borrow().position_center().coords.data } } @@ -364,9 +363,9 @@ impl EventHandler for Game { if self.wall_collision { // if (self.wall_pos_col_y > self.player.pos.y) && // (self.wall_pos_col_x > self.player.pos.x) { - self.world.set_gravity(Vector2::new(0.0, 200.0)); + self.world.set_gravity(Vector2::new(0.0, 200.0)); // } - } + } // else { // self.world.set_gravity(Vector2::new(0.0, -200.0)); // } From 593df0f46311a167bf144891738c1ed421bff782 Mon Sep 17 00:00:00 2001 From: sn99 Date: Thu, 4 Oct 2018 01:39:17 +0530 Subject: [PATCH 2/2] ran rustfmt --- .idea/codeStyles/Project.xml | 29 +++++++++++++ .idea/markdown-navigator.xml | 82 ++++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 +++ src/main.rs | 69 +++++++++++++++++++++--------- 4 files changed, 166 insertions(+), 20 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/markdown-navigator.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..3354e7c --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 479e246..6950fe0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,19 +4,19 @@ extern crate ncollide; extern crate nphysics2d; extern crate rand; -use ggez::{ContextBuilder, Context, GameResult}; use ggez::conf::{WindowMode, WindowSetup}; use ggez::event::{self, EventHandler, Keycode, Mod}; -use ggez::graphics::{Vector2 as gVector2, clear, circle, line, present, DrawMode, Point2}; +use ggez::graphics::{circle, clear, line, present, DrawMode, Point2, Vector2 as gVector2}; use ggez::timer; +use ggez::{Context, ContextBuilder, GameResult}; use rand::Rng; -use na::{Vector1, Vector2, Translation2, UnitComplex}; +use na::{Translation2, UnitComplex, Vector1, Vector2}; use ncollide::shape::{Ball, Plane}; -use nphysics2d::world::World; use nphysics2d::math::Orientation; use nphysics2d::object::{RigidBody, RigidBodyHandle}; +use nphysics2d::world::World; const WINDOW_WIDTH: u32 = 800; const WINDOW_HEIGHT: u32 = 600; @@ -199,7 +199,9 @@ impl Game { // let mut player = RigidBody::new_dynamic(Ball::new(PLAYER_R), 1.0, 0.0, 0.0); player.body.set_inv_mass(1.5); - player.body.append_translation(&Translation2::new(400.0, 300.0)); + player + .body + .append_translation(&Translation2::new(400.0, 300.0)); let player_body = world.add_rigid_body(player.body.clone()); let s = Game { @@ -222,8 +224,12 @@ impl Game { wall.body.set_inv_mass(0.); wall.body.append_rotation(&UnitComplex::new(100.0)); // wall.body.append_rotation(&UnitComplex::new(rand::thread_rng().gen_range(0.0, 100.0))); - wall.body.append_translation(&Translation2::new(0.0, rand::thread_rng().gen_range(200.0, 300.0))); //rand::thread_rng().gen_range(0., 400.0))); - self.wall_bodies.push(self.world.add_rigid_body(wall.body.clone())); + wall.body.append_translation(&Translation2::new( + 0.0, + rand::thread_rng().gen_range(200.0, 300.0), + )); //rand::thread_rng().gen_range(0., 400.0))); + self.wall_bodies + .push(self.world.add_rigid_body(wall.body.clone())); } self.is_walls_setup = true; } @@ -258,7 +264,10 @@ impl Game { println!("player box: {}", self.player.bbox_size); println!("wall box: {}", wall.bbox_size); println!("box size: {}", self.player.bbox_size + wall.bbox_size); - println!("{}", (pdistance.norm() / 1000.0) < (self.player.bbox_size + wall.bbox_size)); + println!( + "{}", + (pdistance.norm() / 1000.0) < (self.player.bbox_size + wall.bbox_size) + ); /* TEST VALUES */ if (pdistance.norm() / 100.) < (self.player.bbox_size + wall.bbox_size) { @@ -300,8 +309,21 @@ impl EventHandler for Game { fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { clear(ctx); let pos = self.player_body.borrow().position_center(); - circle(ctx, DrawMode::Fill, Point2::new(pos.x, pos.y), PLAYER_R, 0.1)?; - line(ctx, &[Point2::new(0.0, FLOOR_Y), Point2::new(WINDOW_WIDTH as f32, FLOOR_Y)], 1.0)?; + circle( + ctx, + DrawMode::Fill, + Point2::new(pos.x, pos.y), + PLAYER_R, + 0.1, + )?; + line( + ctx, + &[ + Point2::new(0.0, FLOOR_Y), + Point2::new(WINDOW_WIDTH as f32, FLOOR_Y), + ], + 1.0, + )?; // Loop over all objects drawing them... { @@ -312,11 +334,17 @@ impl EventHandler for Game { for wall_body in &self.wall_bodies { // let mut coords = wall_body.borrow_mut(); // let local = coords.local_to_world; - line(ctx, &[Point2::new( - wall_body.borrow().position_center().x, - wall_body.borrow().position_center().y), - Point2::new(WINDOW_WIDTH as f32, - -100.0)], 1.0)?; //wall_body.borrow().position_center().coords.data + line( + ctx, + &[ + Point2::new( + wall_body.borrow().position_center().x, + wall_body.borrow().position_center().y, + ), + Point2::new(WINDOW_WIDTH as f32, -100.0), + ], + 1.0, + )?; //wall_body.borrow().position_center().coords.data } } @@ -340,12 +368,13 @@ impl EventHandler for Game { self.world.set_gravity(Vector2::new(0.0, 200.0)); } - let mut player: std::cell::RefMut> = self.player_body.borrow_mut(); + let mut player: std::cell::RefMut> = + self.player_body.borrow_mut(); if player.position_center().y + PLAYER_R > FLOOR_Y - 0.1 { player.apply_central_impulse(Vector2::new(0.0, -150.0)); } } - _ => () + _ => (), } self.refresh_horizontal_vel(); } @@ -370,7 +399,7 @@ impl EventHandler for Game { // self.world.set_gravity(Vector2::new(0.0, -200.0)); // } } - _ => () + _ => (), } self.refresh_horizontal_vel(); } @@ -408,7 +437,7 @@ fn print_instructions() { // let image = assets.actor_image(actor); // let drawparams = ggez::graphics::DrawParam { // dest: pos, -// rotation: actor.direction as f32, //facing var for rotation +// rotation: actor.direction as f32, //facing var for rotation // offset: ggez::graphics::Point2::new(0.5, 0.5), // ..Default::default() // }; @@ -425,4 +454,4 @@ fn world_to_screen_coords(screen_width: u32, screen_height: u32, point: Point2) let x = point.x + width / 2.0; let y = height - (point.y + height / 2.0); Point2::new(x, y) -} \ No newline at end of file +}