main.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #[macro_use]
  2. extern crate specs_derive;
  3. #[macro_use]
  4. extern crate specs_system_macro;
  5. use ggez::graphics::Drawable;
  6. use specs::prelude::*;
  7. use specs::world::WorldExt;
  8. // * constants
  9. const WIDTH: f32 = 640.0;
  10. const HEIGHT: f32 = 480.0;
  11. const MAX_HP: u32 = 100;
  12. // * utility functions
  13. fn clamp(x: f32, min: f32, max: f32) -> f32 {
  14. if x < min {
  15. min
  16. } else if x > max {
  17. max
  18. } else {
  19. x
  20. }
  21. }
  22. // * components
  23. /// This is a component for things which can appear on the screen
  24. #[derive(Component)]
  25. pub struct Drawn {
  26. sprite: Sprite,
  27. }
  28. /// We only have three things which appear: the player, the hostile
  29. /// red dots, and the green 'collision' that appears when they hit
  30. /// each other
  31. pub enum Sprite {
  32. Player,
  33. Hostile,
  34. Kaboom,
  35. }
  36. impl Sprite {
  37. /// Convert one of the pre-known sprites to is intended visual appearance
  38. fn to_mesh(&self, ctx: &mut ggez::Context) -> ggez::GameResult<ggez::graphics::Mesh> {
  39. match self {
  40. Sprite::Player => ggez::graphics::Mesh::new_circle(
  41. ctx,
  42. ggez::graphics::DrawMode::fill(),
  43. [0.0, 0.0],
  44. 10.0,
  45. 0.1,
  46. ggez::graphics::WHITE,
  47. ),
  48. Sprite::Hostile => ggez::graphics::Mesh::new_circle(
  49. ctx,
  50. ggez::graphics::DrawMode::fill(),
  51. [0.0, 0.0],
  52. 10.0,
  53. 0.1,
  54. (1.0, 0.0, 0.0).into(),
  55. ),
  56. Sprite::Kaboom => ggez::graphics::Mesh::new_circle(
  57. ctx,
  58. ggez::graphics::DrawMode::stroke(1.0),
  59. [0.0, 0.0],
  60. 20.0,
  61. 0.1,
  62. (0.0, 1.0, 0.0).into(),
  63. ),
  64. }
  65. }
  66. }
  67. /// A component for things which can be positioned somewhere on the
  68. /// screen
  69. #[derive(Component, Copy, Clone)]
  70. pub struct Position {
  71. x: f32,
  72. y: f32,
  73. }
  74. /// A component for things which move around the screen at a specific
  75. /// velocity
  76. #[derive(Component)]
  77. pub struct Velocity {
  78. dx: f32,
  79. dy: f32,
  80. }
  81. /// A component for things which are player-controlled: that is, the
  82. /// player.
  83. #[derive(Component)]
  84. pub struct Controlled;
  85. /// A component for apply damage events
  86. #[derive(Component)]
  87. pub struct AppliesDamage {
  88. target: specs::Entity,
  89. }
  90. /// A component for things which are ephemeral: when lifetime hits 0,
  91. /// the thing is removed
  92. #[derive(Component)]
  93. pub struct Lifetime {
  94. lifetime: usize,
  95. }
  96. #[derive(Component)]
  97. pub struct HP {
  98. hp: u32,
  99. }
  100. // * KeyState impl
  101. /// A KeyState contains four bools that indicate whether particular
  102. /// keys are held (the only four keys we care about, the famous
  103. /// Gamer's Square: W, A, S, and who can forget D?)
  104. pub struct KeyState {
  105. w_pressed: bool,
  106. a_pressed: bool,
  107. s_pressed: bool,
  108. d_pressed: bool,
  109. }
  110. impl KeyState {
  111. /// By default, none of them are held down
  112. fn new() -> KeyState {
  113. KeyState {
  114. w_pressed: false,
  115. a_pressed: false,
  116. s_pressed: false,
  117. d_pressed: false,
  118. }
  119. }
  120. /// On a key-down event, we might flip some to true
  121. fn handle_down(&mut self, kc: winit::VirtualKeyCode) {
  122. if kc == winit::VirtualKeyCode::W {
  123. self.w_pressed = true;
  124. }
  125. if kc == winit::VirtualKeyCode::A {
  126. self.a_pressed = true;
  127. }
  128. if kc == winit::VirtualKeyCode::S {
  129. self.s_pressed = true;
  130. }
  131. if kc == winit::VirtualKeyCode::D {
  132. self.d_pressed = true;
  133. }
  134. }
  135. /// On a key-up event, we might flip some to false
  136. fn handle_up(&mut self, kc: winit::VirtualKeyCode) {
  137. if kc == winit::VirtualKeyCode::W {
  138. self.w_pressed = false;
  139. }
  140. if kc == winit::VirtualKeyCode::A {
  141. self.a_pressed = false;
  142. }
  143. if kc == winit::VirtualKeyCode::S {
  144. self.s_pressed = false;
  145. }
  146. if kc == winit::VirtualKeyCode::D {
  147. self.d_pressed = false;
  148. }
  149. }
  150. }
  151. // * systems
  152. /// The Draw system, which needs access to the ggez context in order
  153. /// to, uh, draw stuff
  154. struct Draw<'t> {
  155. pub ctx: &'t mut ggez::Context,
  156. }
  157. impl<'a, 't> specs::System<'a> for Draw<'t> {
  158. type SystemData = (
  159. // Position and Drawn are for the usual screen entities
  160. specs::ReadStorage<'a, Position>,
  161. specs::ReadStorage<'a, Drawn>,
  162. // HP and Controlled is so we can display the player HP
  163. specs::ReadStorage<'a, HP>,
  164. specs::ReadStorage<'a, Controlled>,
  165. );
  166. fn run(&mut self, (pos, draw, hp, controlled): Self::SystemData) {
  167. // clear to black
  168. ggez::graphics::clear(self.ctx, ggez::graphics::BLACK);
  169. // draw all the drawable things
  170. for (pos, draw) in (&pos, &draw).join() {
  171. let param = ggez::graphics::DrawParam {
  172. dest: [pos.x, pos.y].into(),
  173. ..ggez::graphics::DrawParam::default()
  174. };
  175. draw.sprite
  176. .to_mesh(self.ctx)
  177. .unwrap()
  178. .draw(self.ctx, param)
  179. .unwrap();
  180. }
  181. // find the HP of the player and print it on the screen
  182. for (hp, _) in (&hp, &controlled).join() {
  183. let player_hp = format!("HP: {}/{}", hp.hp, MAX_HP);
  184. let display = ggez::graphics::Text::new(player_hp);
  185. display.draw(self.ctx, ([50.0, 50.0],).into()).unwrap();
  186. }
  187. // show it, yo
  188. ggez::graphics::present(self.ctx).unwrap();
  189. }
  190. }
  191. // The Control system is for adjusting the current velocity of things
  192. // based on the current state of the keys. The longer you hold keys,
  193. // the more it'll accelerate, up to some fixed speed (20 units per
  194. // tick)
  195. system! {
  196. Control(resource kc: KeyState, _c: Controlled, mut v: Velocity) {
  197. const SPEEDUP: f32 = 0.2;
  198. if kc.w_pressed {
  199. v.dy -= SPEEDUP;
  200. }
  201. if kc.a_pressed {
  202. v.dx -= SPEEDUP;
  203. }
  204. if kc.s_pressed {
  205. v.dy += SPEEDUP;
  206. }
  207. if kc.d_pressed {
  208. v.dx += SPEEDUP;
  209. }
  210. v.dx = clamp(v.dx, -20.0, 20.0);
  211. v.dy = clamp(v.dy, -20.0, 20.0);
  212. }
  213. }
  214. // This applies friction to components, currently by a global amount
  215. system! {
  216. Slowdown(mut v: Velocity, _c: Controlled) {
  217. const ROUNDING: f32 = 0.01;
  218. const FRICTION: f32 = 0.95;
  219. if v.dx.abs() < ROUNDING {
  220. v.dx = 0.0;
  221. } else {
  222. v.dx *= FRICTION;
  223. }
  224. if v.dy.abs() < ROUNDING {
  225. v.dy = 0.0
  226. } else {
  227. v.dy *= FRICTION;
  228. }
  229. }
  230. }
  231. // This moves components around and handles wrapping them
  232. system! {
  233. Move(mut pos: Position, v: Velocity) {
  234. pos.x += v.dx;
  235. pos.y += v.dy;
  236. if pos.x < 0.0 {
  237. pos.x += WIDTH;
  238. } else if pos.x > WIDTH {
  239. pos.x -= WIDTH;
  240. }
  241. if pos.y < 0.0 {
  242. pos.y += HEIGHT;
  243. } else if pos.y > HEIGHT {
  244. pos.y -= HEIGHT;
  245. }
  246. }
  247. }
  248. system_impl! {
  249. Collision(
  250. en: Entity,
  251. resource mut world: ncollide2d::world::CollisionWorld<f32, CollisionData>,
  252. mut pos: Position,
  253. col: Collidable,
  254. mut lifetime: Lifetime,
  255. mut drawn: Drawn,
  256. mut damage: AppliesDamage,
  257. ) {
  258. use nalgebra::Isometry2;
  259. for (p, c) in (&mut pos, &col).join() {
  260. world.get_mut(c.handle).expect("Unable to find collision object")
  261. .set_position(Isometry2::translation(p.x / 10.0, p.y / 10.0));
  262. }
  263. world.update();
  264. for (c1, c2, _, man) in world.contact_pairs(true) {
  265. if let Some(m) = man.deepest_contact() {
  266. let a = m.contact.world1;
  267. let ping = en.create();
  268. lifetime.insert(ping, Lifetime { lifetime: 10 }).unwrap();
  269. drawn.insert(ping, Drawn { sprite: Sprite::Kaboom }).unwrap();
  270. pos.insert(ping, Position {x: a.x * 10.0, y: a.y * 10.0}).unwrap();
  271. if let Some(e1) = world.collision_object(c1).unwrap().data().for_entity {
  272. damage.insert(en.create(), AppliesDamage { target: e1 }).unwrap();
  273. }
  274. if let Some(e2) = world.collision_object(c2).unwrap().data().for_entity {
  275. damage.insert(en.create(), AppliesDamage { target: e2 }).unwrap();
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // This handles lifetimes ticking back down, and removing entities if
  282. // their lifetimes get too low
  283. system_impl! {
  284. TheInexorablePassageOfTime(en: Entity, mut l: Lifetime) {
  285. for (e, mut l) in (&en, &mut l).join() {
  286. l.lifetime -= 1;
  287. if l.lifetime == 0 {
  288. en.delete(e).unwrap();
  289. }
  290. }
  291. }
  292. }
  293. // This handles applying damage to entities, and removing them if
  294. // their HP gets too low.
  295. system_impl! {
  296. TheSlingsAndArrowsOfOutrageousFortune(
  297. en: Entity,
  298. mut dmg: AppliesDamage,
  299. mut hp: HP,
  300. ) {
  301. for dmg in (&dmg).join() {
  302. if let Some(target) = hp.get_mut(dmg.target) {
  303. if target.hp == 0 {
  304. en.delete(dmg.target).unwrap();
  305. }
  306. target.hp -= 1;
  307. }
  308. }
  309. dmg.clear();
  310. }
  311. }
  312. pub struct CollisionData {
  313. for_entity: Option<specs::Entity>,
  314. }
  315. #[derive(Component)]
  316. pub struct Collidable {
  317. handle: ncollide2d::pipeline::CollisionObjectSlabHandle,
  318. }
  319. // * game definition
  320. /// A game just needs a specs world! All our state is in there
  321. struct MyGame {
  322. pub world: specs::World,
  323. }
  324. impl MyGame {
  325. fn setup() -> MyGame {
  326. use nalgebra::Isometry2;
  327. use ncollide2d::pipeline::{CollisionGroups, GeometricQueryType};
  328. use ncollide2d::shape::{Ball, ShapeHandle};
  329. use ncollide2d::world::CollisionWorld;
  330. // this is the first step in making apple pie from scratch
  331. let mut world = specs::World::new();
  332. // register some component types
  333. world.register::<Position>();
  334. world.register::<Drawn>();
  335. world.register::<Controlled>();
  336. world.register::<Velocity>();
  337. world.register::<HP>();
  338. world.register::<Lifetime>();
  339. world.register::<AppliesDamage>();
  340. world.register::<Collidable>();
  341. // create our blank key state
  342. world.insert(KeyState::new());
  343. // create a collision world
  344. let mut collision_world = CollisionWorld::<f32, CollisionData>::new(0.02);
  345. let mut group = CollisionGroups::new();
  346. group.set_membership(&[0]);
  347. group.set_whitelist(&[0]);
  348. let contacts_query = GeometricQueryType::Contacts(0.0, 0.0);
  349. // create a player
  350. let (player_handle, object) = collision_world.add(
  351. Isometry2::translation(20.0, 20.0),
  352. ShapeHandle::new(Ball::new(1.0)),
  353. group,
  354. contacts_query,
  355. CollisionData { for_entity: None },
  356. );
  357. let player = world
  358. .create_entity()
  359. .with(Drawn {
  360. sprite: Sprite::Player,
  361. })
  362. .with(Position { x: 200.0, y: 200.0 })
  363. .with(Controlled)
  364. .with(Velocity { dx: 0.0, dy: 0.0 })
  365. .with(HP { hp: MAX_HP })
  366. .with(Collidable {
  367. handle: player_handle,
  368. })
  369. .build();
  370. object.data_mut().for_entity = Some(player);
  371. // create ten ENEMY ORBS!!!!!!!11111one
  372. for _ in 0..10 {
  373. let x = rand::random::<f32>() * WIDTH;
  374. let y = rand::random::<f32>() * HEIGHT;
  375. let dx = (rand::random::<f32>() * 20.0) - 10.0;
  376. let dy = (rand::random::<f32>() * 20.0) - 10.0;
  377. let (handle, object) = collision_world.add(
  378. Isometry2::translation(x / 10.0, y / 10.0),
  379. ShapeHandle::new(Ball::new(1.0)),
  380. group,
  381. contacts_query,
  382. CollisionData { for_entity: None },
  383. );
  384. let orb = world
  385. .create_entity()
  386. .with(Drawn {
  387. sprite: Sprite::Hostile,
  388. })
  389. .with(Position { x, y })
  390. .with(Velocity { dx, dy })
  391. .with(HP { hp: 50 })
  392. .with(Collidable { handle })
  393. .build();
  394. object.data_mut().for_entity = Some(orb);
  395. }
  396. world.insert(collision_world);
  397. // this is the world I have created. observe it and despair
  398. MyGame { world }
  399. }
  400. }
  401. impl ggez::event::EventHandler for MyGame {
  402. // To draw things, we just run the Draw system
  403. fn draw(&mut self, ctx: &mut ggez::Context) -> ggez::GameResult<()> {
  404. Draw { ctx }.run_now(&self.world);
  405. Ok(())
  406. }
  407. // To update things, we just run a bunch of these systems, and
  408. // make sure we call maintain
  409. fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult<()> {
  410. TheSlingsAndArrowsOfOutrageousFortune.run_now(&self.world);
  411. Slowdown.run_now(&self.world);
  412. Control.run_now(&self.world);
  413. Move.run_now(&self.world);
  414. Collision.run_now(&self.world);
  415. TheInexorablePassageOfTime.run_now(&self.world);
  416. self.world.maintain();
  417. Ok(())
  418. }
  419. // to handle events, we modify the stored KeyState resource
  420. fn key_down_event(
  421. &mut self,
  422. ctx: &mut ggez::Context,
  423. keycode: winit::VirtualKeyCode,
  424. _keymod: ggez::event::KeyMods,
  425. _repeat: bool,
  426. ) {
  427. if keycode == winit::VirtualKeyCode::Escape {
  428. ggez::event::quit(ctx);
  429. }
  430. KeyState::handle_down(&mut self.world.write_resource(), keycode);
  431. }
  432. fn key_up_event(
  433. &mut self,
  434. _ctx: &mut ggez::Context,
  435. keycode: winit::VirtualKeyCode,
  436. _keymod: ggez::event::KeyMods,
  437. ) {
  438. KeyState::handle_up(&mut self.world.write_resource(), keycode);
  439. }
  440. }
  441. // * main
  442. fn main() -> ggez::GameResult<()> {
  443. // And here, we make a context and an event loop
  444. let (mut ctx, mut evloop) = ggez::ContextBuilder::new("game", "me")
  445. // make our window a, uh, size
  446. .window_mode(ggez::conf::WindowMode {
  447. width: WIDTH,
  448. height: HEIGHT,
  449. ..ggez::conf::WindowMode::default()
  450. })
  451. // and build it
  452. .build()?;
  453. // then run the shit yo
  454. let mut my_game = MyGame::setup();
  455. ggez::event::run(&mut ctx, &mut evloop, &mut my_game)
  456. }