From c4cfed13ec6f2c26f20691130655fc6290a44f15 Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Sat, 8 Oct 2022 18:45:07 +0530 Subject: [PATCH] base classes --- Cargo.lock | 411 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ src/asteroids.rs | 52 ++++++ src/main.rs | 27 ++++ src/player.rs | 101 ++++++++++++ 5 files changed, 601 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/asteroids.rs create mode 100644 src/main.rs create mode 100644 src/player.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3aea678 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,411 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + +[[package]] +name = "audir-sles" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea47348666a8edb7ad80cbee3940eb2bccf70df0e6ce09009abe1a836cb779f5" + +[[package]] +name = "audrey" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58b92a84e89497e3cd25d3672cd5d1c288abaac02c18ff21283f17d118b889b8" +dependencies = [ + "dasp_frame", + "dasp_sample", + "hound", + "lewton", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bumpalo" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" + +[[package]] +name = "bytemuck" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f5715e491b5a1598fc2bef5a606847b5dc1d48ea625bd3c02c00de8285591da" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "dasp_frame" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a3937f5fe2135702897535c8d4a5553f8b116f76c1529088797f2eee7c5cd6" +dependencies = [ + "dasp_sample", +] + +[[package]] +name = "dasp_sample" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" + +[[package]] +name = "flate2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fontdue" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a62391ecb864cf12ed06b2af4eda2e609b97657950d6a8f06841b17726ab253" +dependencies = [ + "hashbrown", + "ttf-parser", +] + +[[package]] +name = "genetic" +version = "0.1.0" +dependencies = [ + "macroquad", + "macroquad-particles", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "glam" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518faa5064866338b013ff9b2350dc318e14cc4fcd6cb8206d7e7c9886c98815" + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hound" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d13cdbd5dbb29f9c88095bbdc2590c9cba0d0a1269b983fef6b2cdd7e9f4db1" + +[[package]] +name = "image" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd8e4fb07cf672b1642304e731ef8a6a4c7891d67bb4fd4f5ce58cd6ed86803c" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "num-rational", + "num-traits", + "png", +] + +[[package]] +name = "lewton" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d542c1a317036c45c2aa1cf10cc9d403ca91eb2d333ef1a4917e5cb10628bd0" +dependencies = [ + "byteorder", + "ogg", + "smallvec", +] + +[[package]] +name = "libc" +version = "0.2.134" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" + +[[package]] +name = "macroquad" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19c4f0576d6468cb31de5ba0b3c8eb56cfd95ed3edfee380ac339309b4830074" +dependencies = [ + "bumpalo", + "fontdue", + "glam", + "image", + "macroquad_macro", + "miniquad", + "quad-rand", + "quad-snd", +] + +[[package]] +name = "macroquad-particles" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8505dc035e4d68799a53a459afef0bc345410039325b5928af2e0d83eb15de40" +dependencies = [ + "macroquad", +] + +[[package]] +name = "macroquad_macro" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5cecfede1e530599c8686f7f2d609489101d3d63741a6dc423afc997ce3fcc8" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +dependencies = [ + "libc", +] + +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + +[[package]] +name = "miniquad" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a07975b18f290b99365f042dd80db3e03908539ca6bc47e749c5eef4ee262fd4" +dependencies = [ + "libc", + "ndk-sys", + "objc", + "winapi", +] + +[[package]] +name = "miniz_oxide" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" +dependencies = [ + "adler", +] + +[[package]] +name = "ndk-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121" + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +dependencies = [ + "malloc_buf", +] + +[[package]] +name = "ogg" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13e571c3517af9e1729d4c63571a27edd660ade0667973bfc74a67c660c2b651" +dependencies = [ + "byteorder", +] + +[[package]] +name = "once_cell" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" + +[[package]] +name = "png" +version = "0.17.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f0e7f4c94ec26ff209cee506314212639d6c91b80afb82984819fafce9df01c" +dependencies = [ + "bitflags", + "crc32fast", + "flate2", + "miniz_oxide", +] + +[[package]] +name = "quad-alsa-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66c2f04a6946293477973d85adc251d502da51c57b08cd9c997f0cfd8dcd4b5" +dependencies = [ + "libc", +] + +[[package]] +name = "quad-rand" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658fa1faf7a4cc5f057c9ee5ef560f717ad9d8dc66d975267f709624d6e1ab88" + +[[package]] +name = "quad-snd" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82e2e4a55292a75d8569ef0b3f7c24964074efe5767b359dbf028a0b3c53464" +dependencies = [ + "audir-sles", + "audrey", + "libc", + "quad-alsa-sys", + "winapi", +] + +[[package]] +name = "smallvec" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0" +dependencies = [ + "maybe-uninit", +] + +[[package]] +name = "ttf-parser" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0e0df32 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "genetic" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +macroquad = "0.3.24" +macroquad-particles = "0.1.1" diff --git a/src/asteroids.rs b/src/asteroids.rs new file mode 100644 index 0000000..9c403b1 --- /dev/null +++ b/src/asteroids.rs @@ -0,0 +1,52 @@ +use macroquad::{prelude::*, rand::gen_range}; + +#[derive(Default)] +pub struct Asteroid { + pos: Vec2, + vel: Vec2, + sides: u8, + radius: f32, + rot: f32, + omega: f32, +} + +impl Asteroid { + pub fn new() -> Self { + let mut r = vec2( + if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, + gen_range(-1., 1.), + ); + if gen_range(0., 1.) > 0.5 { + r = vec2(r.y, r.x); + } + r *= vec2(screen_width() / 2. + 100., screen_height() / 2. + 100.); + Self { + pos: r, + vel: vec2( + gen_range(100., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, + gen_range(100., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, + ), + sides: gen_range(3, 8), + radius: gen_range(10., 50.), + omega: gen_range(50., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, + ..Default::default() + } + } + + pub fn update(&mut self) { + self.pos += self.vel * get_frame_time(); + self.rot += self.omega * get_frame_time(); + } + + pub fn draw(&self) { + draw_poly_lines( + self.pos.x, + self.pos.y, + self.sides, + self.radius, + self.rot, + 2., + WHITE, + ); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..96a527c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,27 @@ +mod asteroids; +mod player; + +use asteroids::Asteroid; +use macroquad::prelude::*; +use player::Player; + +#[macroquad::main("Camera")] +async fn main() { + rand::srand(macroquad::miniquad::date::now() as _); + let cam = Camera2D { + zoom: vec2(2. / screen_width(), 2. / screen_height()), + ..Default::default() + }; + let mut asteroid = Asteroid::new(); + set_camera(&cam); + loop { + clear_background(BLACK); + asteroid.update(); + asteroid.draw(); + next_frame().await + } +} + +pub fn rotate_vec(vec: Vec2, angle: f32) -> Vec2 { + vec2(angle.cos(), angle.sin()).rotate(vec) +} diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..2759d29 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,101 @@ +use std::f32::consts::PI; + +use macroquad::{prelude::*, rand::gen_range}; +#[derive(Default)] +pub struct Player { + pos: Vec2, + vel: Vec2, + dir: Vec2, + rot: f32, + drag: f32, + bullets: Vec, + last_shot: f32, + shot_interval: f32, +} + +impl Player { + pub fn new() -> Self { + Self { + dir: vec2(0., 1.), + rot: PI / 2., + drag: 0.001, + shot_interval: 0.3, + ..Default::default() + } + } + + pub fn update(&mut self) { + let mut mag = 0.; + if is_key_down(KeyCode::Right) { + self.rot -= 5. * get_frame_time(); + self.dir = vec2(self.rot.cos(), self.rot.sin()); + } + if is_key_down(KeyCode::Left) { + self.rot += 5. * get_frame_time(); + self.dir = vec2(self.rot.cos(), self.rot.sin()); + } + if is_key_down(KeyCode::Up) { + mag = 360.; + } + if is_key_down(KeyCode::Space) { + if self.shot_interval + self.last_shot < get_time() as f32 { + self.last_shot = get_time() as f32; + self.bullets.push(Bullet { + pos: self.pos + self.dir.rotate(vec2(20., 0.)), + vel: self.dir.rotate(vec2(500., 0.)) + self.vel, + }); + } + } + + self.bullets + .iter_mut() + .filter(|bullet| { + bullet.pos.x.abs() * 2. < screen_width() + && bullet.pos.y.abs() * 2. < screen_height() + }) + .for_each(|bullet| { + bullet.update(); + bullet.draw(); + }); + + self.vel += (mag * self.dir - self.drag * self.vel.length() * self.vel) * get_frame_time(); + self.pos += self.vel * get_frame_time(); + if self.pos.x.abs() > screen_width() / 2. + 10. { + self.pos.x *= -1.; + } + if self.pos.y.abs() > screen_height() / 2. + 10. { + self.pos.y *= -1.; + } + } + + pub fn draw(&self) { + let p1 = self.pos + self.dir.rotate(vec2(20., 0.)); + let p2 = self.pos + self.dir.rotate(vec2(-18., -12.667)); + let p3 = self.pos + self.dir.rotate(vec2(-18., 12.667)); + let p4 = self.pos + self.dir.rotate(vec2(-10., -10.)); + let p5 = self.pos + self.dir.rotate(vec2(-10., 10.)); + let p6 = self.pos + self.dir.rotate(vec2(-25., 0.)); + let p7 = self.pos + self.dir.rotate(vec2(-10., -6.)); + let p8 = self.pos + self.dir.rotate(vec2(-10., 6.)); + draw_line(p1.x, p1.y, p2.x, p2.y, 2., WHITE); + draw_line(p1.x, p1.y, p3.x, p3.y, 2., WHITE); + draw_line(p4.x, p4.y, p5.x, p5.y, 2., WHITE); + if is_key_down(KeyCode::Up) && gen_range(0., 1.) < 0.5 { + draw_triangle_lines(p6, p7, p8, 2., WHITE); + } + } +} + +struct Bullet { + pos: Vec2, + vel: Vec2, +} + +impl Bullet { + fn update(&mut self) { + self.pos += self.vel * get_frame_time(); + } + fn draw(&self) { + draw_circle(self.pos.x, self.pos.y, 2., WHITE); + } +}