summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-03-02 20:35:40 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-03-02 20:35:40 +0200
commit30df4064646fb429568474c0cdb2ee2ee08fd22e (patch)
tree2f444247ef993f2f3a346af747199cec3f2b9b46 /src
parente1ba5ff36cefd602e64210dcfd0c4076c871e38c (diff)
Clickable bugs!
Diffstat (limited to 'src')
-rw-r--r--src/entities/bug.rs34
-rw-r--r--src/entities/mod.rs1
-rw-r--r--src/geometry.rs11
-rw-r--r--src/main.rs46
4 files changed, 81 insertions, 11 deletions
diff --git a/src/entities/bug.rs b/src/entities/bug.rs
new file mode 100644
index 0000000..6b39e8a
--- /dev/null
+++ b/src/entities/bug.rs
@@ -0,0 +1,34 @@
+use geometry::*;
+
+pub struct Bug {
+ pub pos: Vec2d,
+ pub rotation: f64,
+ pub alive: bool
+}
+
+impl Bug {
+ pub fn new(x: f64, y: f64, facing: f64) -> Bug {
+ Bug {
+ pos: Vec2d {
+ x: x,
+ y: y
+ },
+ rotation: facing,
+ alive: true
+ }
+ }
+
+ pub fn advance(&mut self, seconds: f64) {
+ //TODO, add some motion
+ }
+
+ pub fn click(&mut self, point: Vec2d) {
+ if self.touches(point) {
+ self.alive = false;
+ }
+ }
+
+ fn touches(&self, point: Vec2d) -> bool {
+ self.pos.distance(point) <= 45. // Some better hit box modelling might be nice?
+ }
+}
diff --git a/src/entities/mod.rs b/src/entities/mod.rs
new file mode 100644
index 0000000..0f4281c
--- /dev/null
+++ b/src/entities/mod.rs
@@ -0,0 +1 @@
+pub mod bug;
diff --git a/src/geometry.rs b/src/geometry.rs
new file mode 100644
index 0000000..e9d534a
--- /dev/null
+++ b/src/geometry.rs
@@ -0,0 +1,11 @@
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct Vec2d {
+ pub x: f64,
+ pub y: f64
+}
+
+impl Vec2d {
+ pub fn distance(&self, other: Vec2d) -> f64 {
+ ((other.x-self.x).powi(2) + (other.y-self.y).powi(2)).sqrt()
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8d987a4..64ac0c2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,14 +2,20 @@ extern crate gate;
use gate::{App, Audio};
use gate::app_info::AppInfo;
-use gate::input::{InputEvent, KeyCode};
+use gate::input::{InputEvent, KeyCode, MouseButton};
use gate::renderer::{Renderer, Affine};
mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); }
use asset_id::{AssetId, SpriteId, TileId, MusicId, SoundId};
+mod geometry;
+use geometry::*;
+
+mod entities;
+use entities::bug::Bug;
+
struct BugBasherGame {
- bug_pos: (f64, f64)
+ bugs: Vec<Bug>
}
impl App<AssetId> for BugBasherGame {
@@ -17,32 +23,50 @@ impl App<AssetId> for BugBasherGame {
}
fn advance(&mut self, seconds: f64, _audio: &mut Audio<AssetId>) -> bool {
- self.bug_pos.0 = (self.bug_pos.0 + 150. * seconds) % 1000.;
- self.bug_pos.1 = (self.bug_pos.0 / 150.).sin() * 500.;
+ self.bugs.retain(|b| b.alive);
+ for bug in self.bugs.iter_mut() {
+ bug.advance(seconds);
+ }
true
}
fn input(&mut self, evt: InputEvent, _audio: &mut Audio<AssetId>) -> bool {
+ match evt {
+ InputEvent::MousePressed(MouseButton::Left, x, y) => {
+ for bug in self.bugs.iter_mut() {
+ bug.click(Vec2d { x, y });
+ }
+ },
+ _ => {}
+ }
true
}
fn render(&mut self, renderer: &mut Renderer<AssetId>) {
renderer.clear((255,255,255));
- { // drawing sprites
+ {
let mut renderer = renderer.sprite_mode();
- let camera = &Affine::translate(-500., 0.);
- renderer.draw(
- &camera.pre_translate(self.bug_pos.0, self.bug_pos.1),
- SpriteId::Bug
- );
+ for bug in &self.bugs {
+ renderer.draw(
+ &Affine::translate(bug.pos.x, bug.pos.y).pre_rotate(bug.rotation),
+ SpriteId::Bug
+ );
+ }
}
}
}
+impl BugBasherGame {
+}
+
fn main() {
let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
gate::run(info, BugBasherGame {
- bug_pos: (400., 0.)
+ bugs: vec!(
+ Bug::new(0., 0., 0.),
+ Bug::new(500., 200., 0.3),
+ Bug::new(-200., -200., 1.5),
+ )
});
}