From ed3e82dd1a593a2e4dd806e154eb7f13a0dbca01 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 6 Oct 2019 18:17:39 +0900 Subject: [PATCH] Add stars as possible creatable object --- src/main.rs | 172 +++++++++++++++++++++++++++++++++++++++++++----- static/star.png | Bin 0 -> 435 bytes 2 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 static/star.png diff --git a/src/main.rs b/src/main.rs index e70b557..7e9495c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -666,6 +666,32 @@ impl ParticleSystem { } } } + + fn force_spawn(&mut self, count: usize) { + for i in 0..count { + self.particles.push(Particle { + rect: self.host_rect, + circle: self.host_circle, + is_rect: self.is_rect, + velx: (rand::thread_rng() + .gen_range(-PARTICLE_RAND_VEL_RANGE, PARTICLE_RAND_VEL_RANGE) + + self.direction.x) + * self.vel_multiplier, + vely: (rand::thread_rng() + .gen_range(-PARTICLE_RAND_VEL_RANGE, PARTICLE_RAND_VEL_RANGE) + + self.direction.y) + * self.vel_multiplier, + // velx: self.direction.x, + // vely: self.direction.y, + velr: rand::thread_rng() + .gen_range(-PARTICLE_RAND_ROT_RANGE, PARTICLE_RAND_ROT_RANGE) + * self.vel_multiplier, + r: rand::thread_rng().gen_range(0.0, 90.0), + lifetime: self.lifetime, + life_timer: 0.0, + }); + } + } } #[derive(Serialize, Deserialize, Clone)] @@ -908,9 +934,70 @@ impl Planet { } } +#[derive(Clone, Serialize, Deserialize)] +struct Star { + color: Color, + particle_system: ParticleSystem, + velr: f32, + r: f32, +} + +impl Star { + fn new(circle: Circle, color: Color, velr: f32, r: f32) -> Self { + let mut star = Star { + color, + particle_system: ParticleSystem::new( + rand::thread_rng().gen_range(80.0, 200.0), + 850.0, + Rectangle::new((0.0, 0.0), (1.0, 1.0)), + circle, + false, + Vector::new(0.0, 0.0), + color, + 1.0, + 1.0, + ), + velr, + r, + }; + + if star.color.r < 0.75 { + star.color.r = 0.75; + } + if star.color.g < 0.75 { + star.color.g = 0.75; + } + if star.color.b < 0.75 { + star.color.b = 0.75; + } + star.particle_system + .force_spawn(rand::thread_rng().gen_range(20, 45)); + + star + } + + fn update(&mut self, dt: f64) { + self.particle_system.update(dt); + self.r += self.velr * dt as f32; + } + + fn draw(&mut self, image: &mut Image, window: &mut Window, transform: Transform) { + self.particle_system.draw(window, transform); + let mut image_rect = image.area(); + image_rect.pos = self.particle_system.host_circle.pos - image_rect.size / 2.0; + window.draw_ex( + &image_rect, + Blended(image, self.color), + transform * Transform::rotate(self.r), + 1, + ); + } +} + #[derive(Serialize, Deserialize, Clone)] struct SaveData { planets: Vec, + stars: Vec, player: Rectangle, joining_particles: RotatingParticleSystem, } @@ -929,6 +1016,8 @@ struct GameState { s_speak_f: Asset, font: Asset, music2: Asset, + i_star: Option>, + i_star_actual: Option, music_on: bool, music_timer: f64, menu: Menu, @@ -949,6 +1038,7 @@ struct GameState { mouse_pos: Vector, expl_conv_p_systems: Vec, planets: Vec, + stars: Vec, camera: Rectangle, move_to: Vector, save_load_notification: Option, @@ -965,6 +1055,8 @@ impl State for GameState { s_speak_f: Asset::new(Sound::load("speak_f.mp3")), font: Asset::new(Font::load("ClearSans-Regular.ttf")), music2: Asset::new(Sound::load("music2.mp3")), + i_star: Some(Asset::new(Image::load("star.png"))), + i_star_actual: None, music_on: false, music_timer: 0.0, menu: Menu::start(), @@ -1008,6 +1100,7 @@ impl State for GameState { mouse_pos: Vector::new(0.0, 0.0), expl_conv_p_systems: Vec::new(), planets: Vec::new(), + stars: Vec::new(), camera: Rectangle::new((0.0, 0.0), (WIDTH_F, HEIGHT_F)), move_to: Vector::new(400.0, 300.0), save_load_notification: None, @@ -1061,22 +1154,44 @@ impl State for GameState { })?; } else if self.state == 10 { let mut rng = rand::thread_rng(); - let mut expl_conv_system = ExplConvParticleSystem::new( - rng.gen_range(1200.0, 1600.0), - Circle::new(self.mouse_pos, rng.gen_range(15.0, 25.0)), - Color::from_rgba( - rng.gen_range(0x44, 0xFF), - rng.gen_range(0x44, 0xFF), - rng.gen_range(0x44, 0xFF), + let rand_out = rng.gen_range(0.0, 1.0); + if rand_out < 0.75 { + // spawn planet + let mut expl_conv_system = ExplConvParticleSystem::new( + rng.gen_range(1200.0, 1600.0), + Circle::new(self.mouse_pos, rng.gen_range(15.0, 25.0)), + Color::from_rgba( + rng.gen_range(0x44, 0xFF), + rng.gen_range(0x44, 0xFF), + rng.gen_range(0x44, 0xFF), + 1.0, + ), 1.0, - ), - 1.0, - ); - expl_conv_system.activate( - rng.gen_range(13, 40), - rng.gen_range(150.0, 300.0), - ); - self.expl_conv_p_systems.push(expl_conv_system); + ); + expl_conv_system.activate( + rng.gen_range(13, 40), + rng.gen_range(150.0, 300.0), + ); + self.expl_conv_p_systems.push(expl_conv_system); + } else { + // spawn star + let rot_clockwise = rng.gen_bool(0.5); + self.stars.push(Star::new( + Circle::new(self.mouse_pos, rng.gen_range(3.0, 7.0)), + Color::from_rgba( + rng.gen_range(0x58, 0xFF), + rng.gen_range(0x58, 0xFF), + rng.gen_range(0x58, 0xFF), + 1.0, + ), + if rot_clockwise { + rng.gen_range(0.1, 0.3) + } else { + rng.gen_range(-0.3, -0.1) + }, + rng.gen_range(0.0, 90.0), + )); + } self.s_boom.execute(|s| { s.set_volume(0.8); s.play() @@ -1202,6 +1317,7 @@ impl State for GameState { if self.state == 10 { let save_data = SaveData { planets: self.planets.clone(), + stars: self.stars.clone(), player: self.player.clone(), joining_particles: self.joining_particles.clone(), }; @@ -1216,8 +1332,10 @@ impl State for GameState { let load_result = load::("OneAndAll_LD45", "slot0"); if let Ok(save_data) = load_result { self.planets = save_data.planets.clone(); + self.stars = save_data.stars.clone(); self.player = save_data.player.clone(); self.joining_particles = save_data.joining_particles.clone(); + self.expl_conv_p_systems.clear(); self.move_to = self.player.pos; self.camera.pos = self.player.pos - Vector::new(WIDTH_F / 2.0, HEIGHT_F / 2.0); @@ -1354,6 +1472,7 @@ impl State for GameState { self.joining_particles.particle_system.opacity = 0.0; self.expl_conv_p_systems.clear(); self.planets.clear(); + self.stars.clear(); self.player.pos = Vector::new(WIDTH_F / 2.0, HEIGHT_F / 2.0); self.move_to = Vector::new(WIDTH_F / 2.0, HEIGHT_F / 2.0); self.camera.pos = Vector::new(0.0, 0.0); @@ -1506,6 +1625,9 @@ impl State for GameState { for planet in &mut self.planets { planet.update(dt); } + for star in &mut self.stars { + star.update(dt); + } if let Some(sl) = &mut self.save_load_notification { match sl { @@ -1538,6 +1660,20 @@ impl State for GameState { } } + if self.i_star_actual.is_none() { + if let Some(i_s) = &mut self.i_star { + let mut star: Option = None; + i_s.execute(|i| { + star = Some(i.clone()); + Ok(()) + })?; + self.i_star_actual = star; + } + if self.i_star_actual.is_some() { + self.i_star = None; + } + } + Ok(()) } @@ -1621,6 +1757,12 @@ impl State for GameState { planet.draw(window, Transform::IDENTITY); } + if let Some(i) = &mut self.i_star_actual { + for star in &mut self.stars { + star.draw(i, window, Transform::IDENTITY); + } + } + if let Some(sl) = &mut self.save_load_notification { match sl { SaveLoadNotification::Save { text, timer } diff --git a/static/star.png b/static/star.png new file mode 100644 index 0000000000000000000000000000000000000000..9e116bf2fec4750c46f3ee30e5a237113bd3a17d GIT binary patch literal 435 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzwj^(N7a$D;Kb?2i11Zh|kH}&M z20djEW~^9hUj`IpFY)wsWq-`Y#v!56mGtx#P)M@GHKN2hKQ}iuuY|$5C^fMpHASI3 zvm`^o-P1Q9ypd0wfq_xl)5S3);_%VwhDC=QcwF~OYApOL>*V@eZ|Y8mKG&j24hMal z{0^`#P-+cc{y;#Vb@q{D1Buhwe`m^uf2-GCtGYHtz<{Z3T0W1CzPH!mEM4KfOWHsD z`POwHIl_+V+ktbZZ63$ZWIs^NnQ(b~<++`o_MJKIP{{azTjIExe}n4RC?-#z2G;Fg zg|eUfp82m})$MZP=WE$#`=c0zS69XrKbWwVlw+aC+H+;<3S`XGIhso{NqltP)r@uJsfGji{?h$g(|{h-OI?UK#t+;jc2 Ze1B{GWJd=79AH>7c)I$ztaD0e0suRIsu}