2022-10-08 13:15:07 +00:00
|
|
|
mod asteroids;
|
2022-10-09 18:14:22 +00:00
|
|
|
mod nn;
|
2022-10-08 13:15:07 +00:00
|
|
|
mod player;
|
2022-10-09 20:11:24 +00:00
|
|
|
mod population;
|
2022-10-08 15:27:05 +00:00
|
|
|
mod world;
|
2022-10-08 13:15:07 +00:00
|
|
|
|
2023-01-07 21:59:59 +00:00
|
|
|
use std::borrow::BorrowMut;
|
|
|
|
|
|
|
|
use macroquad::{
|
|
|
|
prelude::*,
|
|
|
|
ui::{hash, root_ui, widgets, Skin},
|
|
|
|
};
|
2022-10-10 18:36:14 +00:00
|
|
|
use population::Population;
|
2022-10-08 13:15:07 +00:00
|
|
|
|
2023-01-06 22:47:57 +00:00
|
|
|
pub const WIDTH: f32 = 800.;
|
|
|
|
pub const HEIGHT: f32 = 780.;
|
|
|
|
|
|
|
|
fn window_conf() -> Conf {
|
|
|
|
Conf {
|
|
|
|
window_title: "Asteroids".to_string(),
|
|
|
|
// fullscreen: true,
|
|
|
|
window_width: 1400,
|
|
|
|
window_height: 800,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[macroquad::main(window_conf)]
|
2022-10-08 13:15:07 +00:00
|
|
|
async fn main() {
|
|
|
|
rand::srand(macroquad::miniquad::date::now() as _);
|
2023-01-07 21:59:59 +00:00
|
|
|
let pause = load_texture("pause.png").await.unwrap();
|
|
|
|
let play = load_texture("play.png").await.unwrap();
|
|
|
|
let fast = load_texture("fast.png").await.unwrap();
|
|
|
|
let restart = load_texture("restart.png").await.unwrap();
|
2023-01-06 22:47:57 +00:00
|
|
|
let th = (screen_height() - HEIGHT) * 0.5;
|
|
|
|
|
|
|
|
let gamecam = Camera2D {
|
2022-10-09 05:40:56 +00:00
|
|
|
zoom: vec2(2. / screen_width(), -2. / screen_height()),
|
2023-01-06 22:47:57 +00:00
|
|
|
offset: vec2((2. * th + WIDTH) / screen_width() - 1., 0.),
|
2022-10-08 13:15:07 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-01-06 22:47:57 +00:00
|
|
|
let maincam = Camera2D {
|
|
|
|
zoom: vec2(2. / screen_width(), -2. / screen_height()),
|
|
|
|
offset: vec2(
|
|
|
|
(th + WIDTH) / screen_width(),
|
|
|
|
-((th + HEIGHT) * 0.5) / screen_height(),
|
|
|
|
),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
// let mut cam = Camera2D::from_display_rect(Rect {
|
|
|
|
// x: 0.,
|
|
|
|
// y: 0.,
|
|
|
|
// w: 1600.,
|
|
|
|
// h: 1200.,
|
|
|
|
// });
|
|
|
|
// cam.offset = vec2(1., -1.);
|
|
|
|
// // {
|
|
|
|
// zoom: vec2(2. / 800., -2. / 600.),
|
|
|
|
// // offset: vec2(-19. / 60., 0.),
|
|
|
|
// ..Default::default()
|
|
|
|
// };
|
2022-10-22 19:40:19 +00:00
|
|
|
let mut pop = Population::new(100);
|
2022-10-10 19:43:05 +00:00
|
|
|
let mut speedup = false;
|
2023-01-07 21:59:59 +00:00
|
|
|
let mut paused = false;
|
|
|
|
let mut checkbox = false;
|
|
|
|
let mut combobox = 0;
|
|
|
|
let mut text = String::new();
|
|
|
|
let mut number = 0.0;
|
|
|
|
|
|
|
|
let skin = {
|
|
|
|
let boxed = root_ui()
|
|
|
|
.style_builder()
|
|
|
|
.background(Image {
|
|
|
|
width: 3,
|
|
|
|
height: 3,
|
|
|
|
bytes: vec![
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255,
|
|
|
|
],
|
|
|
|
})
|
|
|
|
.background_margin(RectOffset::new(1., 1., 1., 1.));
|
|
|
|
|
|
|
|
let window_style = root_ui()
|
|
|
|
.style_builder()
|
|
|
|
.background(Image {
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
bytes: vec![0; 4],
|
|
|
|
})
|
|
|
|
.background_margin(RectOffset::new(0., 0., 0., 0.))
|
|
|
|
.build();
|
|
|
|
let button_style = boxed
|
|
|
|
.color_hovered(RED)
|
|
|
|
.color_clicked(BLUE)
|
|
|
|
.text_color(WHITE)
|
|
|
|
.text_color_hovered(WHITE)
|
|
|
|
.text_color_clicked(WHITE)
|
|
|
|
.margin(RectOffset::new(10., 10., 8., 8.))
|
|
|
|
.build();
|
|
|
|
let label_style = root_ui()
|
|
|
|
.style_builder()
|
|
|
|
.text_color(WHITE)
|
|
|
|
.font_size(24)
|
|
|
|
.margin(RectOffset::new(5., 5., 4., 4.))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
Skin {
|
|
|
|
window_style,
|
|
|
|
button_style,
|
|
|
|
label_style,
|
|
|
|
margin: 0.,
|
|
|
|
..root_ui().default_skin()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
root_ui().push_skin(&skin);
|
2022-10-08 13:15:07 +00:00
|
|
|
loop {
|
2022-10-10 10:13:06 +00:00
|
|
|
clear_background(BLACK);
|
2022-10-10 19:43:05 +00:00
|
|
|
if is_key_pressed(KeyCode::S) {
|
|
|
|
speedup = !speedup;
|
|
|
|
}
|
|
|
|
if speedup {
|
2023-01-07 21:59:59 +00:00
|
|
|
if !paused {
|
|
|
|
for _ in 0..1000 {
|
|
|
|
pop.update();
|
|
|
|
}
|
2022-10-10 19:43:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-07 21:59:59 +00:00
|
|
|
if !paused {
|
|
|
|
pop.update();
|
|
|
|
}
|
2022-10-10 19:43:05 +00:00
|
|
|
pop.draw();
|
|
|
|
}
|
2023-01-06 22:47:57 +00:00
|
|
|
draw_rectangle_lines(-WIDTH * 0.5, -HEIGHT * 0.5, WIDTH, HEIGHT, 2., WHITE);
|
2023-01-07 21:59:59 +00:00
|
|
|
draw_rectangle_lines(
|
|
|
|
WIDTH * 0.5 + th,
|
|
|
|
-HEIGHT * 0.5,
|
|
|
|
screen_width() - WIDTH - 3. * th,
|
|
|
|
34.,
|
|
|
|
2.,
|
|
|
|
WHITE,
|
|
|
|
);
|
|
|
|
draw_rectangle_lines(
|
|
|
|
WIDTH * 0.5 + th,
|
|
|
|
-HEIGHT * 0.5 + (screen_height() - 3. * th) * 0.5 - 34.,
|
|
|
|
screen_width() - WIDTH - 3. * th,
|
|
|
|
34.,
|
|
|
|
2.,
|
|
|
|
WHITE,
|
|
|
|
);
|
2023-01-06 22:47:57 +00:00
|
|
|
|
|
|
|
set_camera(&maincam);
|
|
|
|
// draw_circle(0., 0., 20., RED);
|
2023-01-07 21:59:59 +00:00
|
|
|
pop.worlds[0].player.draw_brain(
|
2023-01-06 22:47:57 +00:00
|
|
|
screen_width() - WIDTH - 3. * th,
|
|
|
|
(screen_height() - 3. * th) * 0.5,
|
|
|
|
);
|
2023-01-07 21:59:59 +00:00
|
|
|
|
2023-01-06 22:47:57 +00:00
|
|
|
set_camera(&gamecam);
|
2023-01-07 21:59:59 +00:00
|
|
|
root_ui().window(
|
|
|
|
hash!(),
|
|
|
|
vec2(WIDTH + 2. * th, th),
|
|
|
|
vec2(screen_width() - WIDTH - 3. * th + 1., 34.),
|
|
|
|
|ui| {
|
|
|
|
ui.label(None, &format!("Generation: {}", pop.gen));
|
|
|
|
ui.same_line(278.);
|
|
|
|
widgets::Button::new("Load Model").ui(ui);
|
|
|
|
ui.same_line(0.);
|
|
|
|
widgets::Button::new("Save Model").ui(ui);
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(fast).ui(ui) {
|
|
|
|
speedup = !speedup;
|
|
|
|
};
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(restart).ui(ui) {
|
|
|
|
pop = Population::new(100);
|
|
|
|
};
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(if paused { play } else { pause }).ui(ui) {
|
|
|
|
paused = !paused;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
root_ui().window(
|
|
|
|
hash!(),
|
|
|
|
vec2(WIDTH + 2. * th, (screen_height() - th) * 0.5 - 34.),
|
|
|
|
vec2(screen_width() - WIDTH - 3. * th + 1., 34.),
|
|
|
|
|ui| {
|
|
|
|
ui.label(None, &format!("Generation: {}", pop.gen));
|
|
|
|
ui.same_line(278.);
|
|
|
|
widgets::Button::new("Load Model").ui(ui);
|
|
|
|
ui.same_line(0.);
|
|
|
|
widgets::Button::new("Save Model").ui(ui);
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(fast).ui(ui) {
|
|
|
|
speedup = !speedup;
|
|
|
|
};
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(restart).ui(ui) {
|
|
|
|
pop = Population::new(100);
|
|
|
|
};
|
|
|
|
ui.same_line(0.);
|
|
|
|
if widgets::Button::new(if paused { play } else { pause }).ui(ui) {
|
|
|
|
paused = !paused;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
2023-01-06 22:47:57 +00:00
|
|
|
|
|
|
|
// set_camera(&maincam);
|
|
|
|
// draw_texture_ex(
|
|
|
|
// target.texture,
|
|
|
|
// 0.,
|
|
|
|
// 0.,
|
|
|
|
// Color::new(1., 1., 1., 0.3),
|
|
|
|
// DrawTextureParams {
|
|
|
|
// flip_y: true,
|
|
|
|
// ..Default::default()
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// set_camera(&cam);
|
|
|
|
|
|
|
|
next_frame().await;
|
2022-10-08 13:15:07 +00:00
|
|
|
}
|
|
|
|
}
|