use crate::core::resources::Resources;
use crate::ui::grid::Grid;
use crate::plants::Plant;
use crate::entities::pea::Pea;
use crate::zombies::Zombie;
use crate::entities::sun::Sun;
use crate::ui::shop::Shop;
use ggez::{Context, GameResult};
use ggez::graphics::{self, Color, DrawParam, Text, TextFragment};
pub struct Renderer;
impl Renderer {
pub fn draw_game(
ctx: &mut Context,
resources: &Resources,
grid: &Grid,
plants: &[Plant],
peas: &[Pea],
zombies: &[Zombie],
suns: &[Sun],
shop: &Shop,
sun_count: i32,
game_over: bool
) -> GameResult {
graphics::clear(ctx, Color::WHITE);
Renderer::draw_background(ctx, resources)?;
grid.draw(ctx)?;
for plant in plants {
plant.draw(ctx, resources)?;
}
for pea in peas {
pea.draw(ctx, resources)?;
}
for zombie in zombies {
zombie.draw(ctx, resources)?;
}
for sun in suns {
sun.draw(ctx, resources)?;
}
Renderer::draw_ui(ctx, resources, shop, sun_count)?;
if game_over {
Renderer::draw_game_over(ctx)?;
}
graphics::present(ctx)?;
Ok(())
}
fn draw_background(ctx: &mut Context, resources: &Resources) -> GameResult {
graphics::draw(ctx, &resources.background, DrawParam::default())?;
graphics::draw(
ctx,
&resources.shop_image,
DrawParam::default().dest([250.0, 0.0])
)?;
Ok(())
}
fn draw_ui(ctx: &mut Context, resources: &Resources, shop: &Shop, sun_count: i32) -> GameResult {
shop.draw(ctx, resources)?;
let sun_text = Text::new(
TextFragment::new(format!("{}", sun_count))
.color(Color::BLACK)
.scale(25.0)
);
graphics::draw(
ctx,
&sun_text,
DrawParam::default().dest([285.0, 65.0])
)?;
Ok(())
}
fn draw_game_over(ctx: &mut Context) -> GameResult {
let game_over_text = Text::new(
TextFragment::new("GAME OVER")
.color(Color::RED)
.scale(100.0)
);
let text_width = game_over_text.width(ctx);
let text_height = game_over_text.height(ctx);
let screen_size = graphics::drawable_size(ctx);
graphics::draw(
ctx,
&game_over_text,
DrawParam::default().dest([
screen_size.0 / 2.0 - text_width / 2.0,
screen_size.1 / 2.0 - text_height / 2.0,
])
)?;
Ok(())
}
}