use ggez::{Context, GameResult};
use ggez::graphics::{self, DrawParam};
use crate::core::resources::Resources;
pub enum SunType {
NaturalGeneration,
SunflowerGeneration,
}
pub struct Sun {
x: f32,
y: f32,
target_y: f32,
speed: f32,
lifetime: u64,
animation_frame: usize,
animation_timer: u64,
sun_type: SunType,
initial_y: f32,
jump_height: f32,
jump_phase: f32,
jump_speed: f32,
}
impl Sun {
pub fn new(x: f32, y: f32, gen_sun_type:SunType) -> Self {
let target_y = rand::random::<f32>() * 400.0 + 200.0;
Sun {
x,
y,
target_y,
speed: 0.06,
lifetime: 0,
animation_frame: 0,
animation_timer: 0,
sun_type: gen_sun_type,
initial_y: y,
jump_height: 20.0, jump_phase: 0.0, jump_speed: 0.2, }
}
pub fn update(&mut self, dt: u64) {
match self.sun_type {
SunType::NaturalGeneration => {
if self.y < self.target_y {
self.y += self.speed * dt as f32;
if self.y > self.target_y {
self.y = self.target_y;
}
}
}
SunType::SunflowerGeneration => {
if self.jump_phase < 100.0 {
self.jump_phase += self.jump_speed * dt as f32;
let progress = self.jump_phase.min(100.0) / 100.0;
let parabola = -4.0 * (progress - 0.5) * (progress - 0.5) + 1.0;
let current_jump_height = parabola * self.jump_height;
self.y = self.initial_y - current_jump_height;
}
}
}
self.lifetime += dt;
self.animation_timer += dt;
if self.animation_timer > 50 { self.animation_frame = (self.animation_frame + 1) % 22; self.animation_timer = 0;
}
}
pub fn draw(&self, ctx: &mut Context, resources: &Resources) -> GameResult {
graphics::draw(
ctx,
&resources.sun_images[self.animation_frame], DrawParam::default()
.dest([self.x, self.y])
.scale([0.6, 0.6]),
)
}
pub fn contains_point(&self, x: f32, y: f32) -> bool {
let radius = 40.0; let dx = self.x + radius - x;
let dy = self.y + radius - y;
dx * dx + dy * dy <= radius * radius
}
}