schule-pong/src/engine/objs/primitive_shapes.rs

79 lines
1.6 KiB
Rust

use super::{
geometry::{Position, Size},
obj_traits::{MovingObject, Object},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Rect {
pos: Position,
size: Size,
}
impl Rect {
pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
Self {
pos: Position::new(x, y),
size: Size::new(width, height),
}
}
pub fn square(x: u32, y: u32, size: u32) -> Self {
Self {
pos: Position::new(x, y),
size: Size::new(size, size),
}
}
}
impl Object for Rect {
fn position(&self) -> Position {
self.pos
}
fn size(&self) -> Size {
self.size
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MovingRect {
prev_pos: Position,
current: Rect,
}
impl MovingRect {
pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
Self {
prev_pos: Position::new(x, y),
current: Rect::new(x, y, width, height),
}
}
pub fn square(x: u32, y: u32, size: u32) -> Self {
Self {
prev_pos: Position::new(x, y),
current: Rect::square(x, y, size),
}
}
}
impl Object for MovingRect {
fn position(&self) -> Position {
self.current.position()
}
fn size(&self) -> Size {
self.current.size()
}
}
impl MovingObject for MovingRect {
fn previous_pos(&self) -> Position {
self.prev_pos
}
fn update_pos(&mut self, x: u32, y: u32) {
self.prev_pos = self.current.pos;
self.current.pos = Position::new(x, y);
}
}