summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-03-06 21:05:29 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-03-06 21:05:29 +0200
commit544c030b80d4d7b9a3b00711ff21b2592275a7e8 (patch)
tree2323fa63649fdaaec9e1cb5a278a81913969960c
parent30df4064646fb429568474c0cdb2ee2ee08fd22e (diff)
Bugs walking to the middle of the screen slowly
-rw-r--r--src/entities/bug.rs12
-rw-r--r--src/geometry.rs38
-rw-r--r--src/main.rs11
3 files changed, 57 insertions, 4 deletions
diff --git a/src/entities/bug.rs b/src/entities/bug.rs
index 6b39e8a..d2d5c2e 100644
--- a/src/entities/bug.rs
+++ b/src/entities/bug.rs
@@ -6,6 +6,8 @@ pub struct Bug {
pub alive: bool
}
+const SPEED: f64 = 75.;
+
impl Bug {
pub fn new(x: f64, y: f64, facing: f64) -> Bug {
Bug {
@@ -19,7 +21,13 @@ impl Bug {
}
pub fn advance(&mut self, seconds: f64) {
- //TODO, add some motion
+ self.rotation = (-self.pos).angle();
+ let distance = SPEED*seconds;
+ let delta_pos = Vec2d {
+ x: distance * self.rotation.cos(),
+ y: distance * self.rotation.sin()
+ };
+ self.pos = self.pos + delta_pos;
}
pub fn click(&mut self, point: Vec2d) {
@@ -29,6 +37,8 @@ impl Bug {
}
fn touches(&self, point: Vec2d) -> bool {
+ let rx = 35.;
+ let ry = 16.;
self.pos.distance(point) <= 45. // Some better hit box modelling might be nice?
}
}
diff --git a/src/geometry.rs b/src/geometry.rs
index e9d534a..be903d7 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -1,3 +1,5 @@
+use std::ops::*;
+
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vec2d {
pub x: f64,
@@ -8,4 +10,40 @@ impl Vec2d {
pub fn distance(&self, other: Vec2d) -> f64 {
((other.x-self.x).powi(2) + (other.y-self.y).powi(2)).sqrt()
}
+ pub fn angle(&self) -> f64 {
+ self.y.atan2(self.x)
+ }
+}
+
+impl Add for Vec2d {
+ type Output = Vec2d;
+
+ fn add(self, other: Self) -> Self {
+ Vec2d {
+ x: self.x + other.x,
+ y: self.y + other.y
+ }
+ }
+}
+
+impl Sub for Vec2d {
+ type Output = Vec2d;
+
+ fn sub(self, other: Self) -> Self {
+ Vec2d {
+ x: self.x - other.x,
+ y: self.y - other.y
+ }
+ }
+}
+
+impl Neg for Vec2d {
+ type Output = Vec2d;
+
+ fn neg(self) -> Self {
+ Vec2d {
+ x: -self.x,
+ y: -self.y
+ }
+ }
}
diff --git a/src/main.rs b/src/main.rs
index 64ac0c2..960197f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,11 +2,11 @@ extern crate gate;
use gate::{App, Audio};
use gate::app_info::AppInfo;
-use gate::input::{InputEvent, KeyCode, MouseButton};
+use gate::input::*;
use gate::renderer::{Renderer, Affine};
mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); }
-use asset_id::{AssetId, SpriteId, TileId, MusicId, SoundId};
+use asset_id::*;
mod geometry;
use geometry::*;
@@ -64,9 +64,14 @@ fn main() {
let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
gate::run(info, BugBasherGame {
bugs: vec!(
- Bug::new(0., 0., 0.),
Bug::new(500., 200., 0.3),
Bug::new(-200., -200., 1.5),
+ Bug::new(-1000., 200., 0.),
+ Bug::new(1200., 0., 0.3),
+ Bug::new(-1300., 0., 1.5),
+ Bug::new(0., 1100., 0.),
+ Bug::new(0., -1500., 0.3),
+ Bug::new(300., -1200., 1.5),
)
});
}