use crate::graphics::graphic_block::{GraphicBlock, Position};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::Widget;
use ratatui::style::Style;
use ratatui::widgets::WidgetRef;
pub const FRUITS_SCORES_PROBABILITIES: &[(&str, i32, u16, i16)] = &[
("🦞", -50, 5, -150),
("🥥", -10, 5, -40),
("🍇", 5, 4, 0),
("🍐", 10, 10, 5),
("🥝", 20, 10, 8),
("🍋", 30, 15, 10),
("🍌", 40, 15, 15),
("🍉", 50, 15, 15),
("🍎", 75, 15, 15),
("🍓", 100, 5, 20),
("🍒", 200, 1, 25),
];
#[derive(PartialEq, Debug, Clone)]
pub struct Fruit<'a> {
score: i32,
grow_snake: i16,
graphic_block: GraphicBlock<'a>,
}
impl<'a> Fruit<'a> {
#[must_use]
pub fn new(
score: i32,
grow_snake_by_relative_nb: i16,
position: Position,
image: &'a str,
) -> Fruit<'a> {
Self {
score,
grow_snake: grow_snake_by_relative_nb,
graphic_block: GraphicBlock::new(position, image, Style::default()),
}
}
#[must_use]
pub fn is_at_position(&self, position: &Position) -> bool {
self.graphic_block.get_position() == position
}
pub fn set_position(&mut self, position: Position) {
self.graphic_block.set_position(position);
}
#[must_use]
pub fn get_score(&self) -> i32 {
self.score
}
#[must_use]
pub fn get_grow_snake(&self) -> i16 {
self.grow_snake
}
}
impl Widget for Fruit<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.graphic_block.render(area, buf);
}
}
impl WidgetRef for Fruit<'_> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
self.graphic_block.render_ref(area, buf);
}
}