summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entities/bug.rs24
-rw-r--r--src/geometry.rs26
-rw-r--r--src/main.rs3
3 files changed, 38 insertions, 15 deletions
diff --git a/src/entities/bug.rs b/src/entities/bug.rs
index 913253c..f64669d 100644
--- a/src/entities/bug.rs
+++ b/src/entities/bug.rs
@@ -5,31 +5,29 @@ use hitbox::*;
pub struct Bug {
pub pos: Vec2d,
pub rotation: f64,
+ pub velocity: Vec2d,
pub alive: bool
}
const SPEED: f64 = 175.;
impl Bug {
- pub fn new(x: f64, y: f64, facing: f64) -> Bug {
+ pub fn new(x: f64, y: f64) -> Bug {
+ let pos = Vec2d {
+ x: x,
+ y: y
+ };
+ let pos_unit = pos.unit();
Bug {
- pos: Vec2d {
- x: x,
- y: y
- },
- rotation: facing,
+ pos: pos,
+ rotation: (-pos).angle(),
+ velocity: -pos_unit * SPEED,
alive: true
}
}
pub fn advance(&mut self, seconds: f64) {
- 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;
+ self.pos = self.pos + self.velocity * seconds;
}
}
diff --git a/src/geometry.rs b/src/geometry.rs
index c49fe2a..f84a973 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -14,9 +14,24 @@ impl Vec2d {
((other.x-self.x).powi(2) + (other.y-self.y).powi(2))
}
+ pub fn magnitude(&self) -> f64 {
+ self.magnitude_squared().sqrt()
+ }
+ pub fn magnitude_squared(&self) -> f64 {
+ self.x.powi(2) + self.y.powi(2)
+ }
+
pub fn angle(&self) -> f64 {
self.y.atan2(self.x)
}
+
+ pub fn unit(&self) -> Vec2d {
+ let mag = self.magnitude();
+ Vec2d {
+ x: self.x / mag,
+ y: self.y / mag
+ }
+ }
}
impl Add for Vec2d {
@@ -51,3 +66,14 @@ impl Neg for Vec2d {
}
}
}
+
+impl Mul<f64> for Vec2d {
+ type Output = Vec2d;
+
+ fn mul(self, rhs: f64) -> Self {
+ Vec2d {
+ x: self.x * rhs,
+ y: self.y * rhs
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 98dd39f..857e744 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -66,8 +66,7 @@ impl App<AssetId> for BugBasherGame {
let angle = angle_dist.ind_sample(&mut self.rng);
self.bugs.push(Bug::new(
angle.cos()*1000.,
- angle.sin()*1000.,
- angle + PI
+ angle.sin()*1000.
));
}