Witam
Napisałem strukturę Rectangle która ma metode update która przyjmuje &mut self
oraz &vec<Rectangle>
. Chodzi o to że muszę po tym jak wyciągnę referencje mutowalną potrzebuje jeszcze referencji do vectora<Rectangle>
i wtedy jest problem z tym checker borrowem. Pisałem wcześniej dużo rzeczy obiektowo. Może da się to innaczej rozgryźć lub obejsć jakimiś metodami która ma już zaimplementowane RUST ale nic nie moge znaleźc bądz jestem słaby w szukaniu w necie.
use std::{default, cell::RefCell, rc::Rc, fmt::Display, borrow::BorrowMut, sync::Arc};
use macroquad::prelude::*;
use rand::{*, distributions::{Uniform, uniform::SampleBorrow}};
extern crate rand;
trait Draw {
fn draw(&self);
}
#[derive(Default)]
struct Rectangle {
position: Vec2,
size: Vec2,
directionHorizontal: bool,
directionVertical: bool,
color: Color,
}
impl Rectangle {
fn new(position:Vec2, size:Vec2, directionHorizontal: bool, directionVertical: bool, color:Color) -> Self{
Self { position, size, directionHorizontal, directionVertical, color}
}
fn update(&mut self,vec:&Vec<Rectangle>){
if self.directionHorizontal {
for rect in vec {
if Game::collisionR(self, rect, 1., 0.) {
self.directionHorizontal = false;
break;
}
}
if self.directionHorizontal{
self.position.x += 1.;
}
}
else {
}
if self.directionVertical {
}
else {
}
}
fn draw(&self) {
draw_rectangle(self.position.x, self.position.y, self.size.x, self.size.y, self.color);
}
}
#[derive(Default)]
struct Game {
WIDTH_CANVAS:u32,
HEIGHT_CANVAS:u32
}
impl Game{
fn new(counts_rects: i32, WIDTH_CANVAS:u32, HEIGHT_CANVAS:u32, range_size:Vec2,rectangles: &mut Vec<Rectangle>) -> Self {
let mut rng = rand::thread_rng();
for i in 0..counts_rects {
loop {
let size_rect = rng.sample(Uniform::from(range_size.x..range_size.y));
let size = Vec2 {x: size_rect, y: size_rect };
let temp = Rectangle {
position: Vec2 { x: rng.sample(Uniform::from(0..WIDTH_CANVAS-size.x as u32)) as f32, y: rng.sample(Uniform::from(0..HEIGHT_CANVAS-size.y as u32)) as f32},
size,
directionHorizontal: random(),
directionVertical: random(),
color: Color::from_rgba(random(),random(),random(),255),
};
let mut find_collision = false;
for index in rectangles.iter_mut() {
if Self::collision(&index, &temp){
find_collision = true;
break;
}
}
if rectangles.len() == 0 || !find_collision{
rectangles.push(temp);
break;
}
}
}
Self {WIDTH_CANVAS,HEIGHT_CANVAS}
}
fn collision(r1:& Rectangle, r2:& Rectangle) -> bool {
if
r1.position.x + r1.size.x > r2.position.x &&
r1.position.x < r2.position.x + r2.size.x&&
r1.position.y + r1.size.y > r2.position.y &&
r1.position.y < r2.position.y + r2.size.y {
return true;
}
return false;
}
fn collisionR(r1:& Rectangle, r2:& Rectangle, shift_x:f32, shift_y:f32) -> bool {
if
r1.position.x + r1.size.x > r2.position.x &&
r1.position.x < r2.position.x + r2.size.x&&
r1.position.y + r1.size.y > r2.position.y &&
r1.position.y < r2.position.y + r2.size.y {
return true;
}
return false;
}
}
#[macroquad::main("Arkanoid")]
async fn main() {
let mut vec:Vec<Rectangle> = Vec::new();
let mut game = Game::new(2000, 800, 600, Vec2 {x:1.,y:50.}, &mut vec);
loop {
clear_background(BLACK);
for i in vec.iter_mut() {
i.update(&vec)
}
//TUTAJ POWYŻEJ JEST BŁĄD NIE POTRAWIE TEGO NAPRAWIĆ//
for i in vec.iter() {
i.draw();
}
next_frame().await
}
}