Added a map (wich is a little buggy when trying to set it by hand) and a randomiszer to it and debug now shows limits
This commit is contained in:
+133
-4
@@ -5,9 +5,10 @@ use sdl2::keyboard::Scancode;
|
||||
use std::time::Duration;
|
||||
use sdl2::rect::Point;
|
||||
use sdl2::rect::Rect;
|
||||
use rand::Rng;
|
||||
|
||||
const WINDOW_HEIGHT:i32 = 800;
|
||||
const WINDOW_WIDTH:i32 = 500;
|
||||
const WINDOW_HEIGHT:i32 = 1000;
|
||||
const WINDOW_WIDTH:i32 = 800;
|
||||
const BALL_SPEED:f32 = 3.0;
|
||||
const MAX_SPEED:f32 = 60.0;
|
||||
const DEBUG_FX_SIZE_RATIO:i32 = 2;
|
||||
@@ -17,18 +18,140 @@ const DEFAULT_BALL_WIDTH:i32 = 20;
|
||||
const DEFAULT_START_BALL_LOCATION_X:i32 = WINDOW_WIDTH / 2 - DEFAULT_BALL_WIDTH / 2;
|
||||
const DEFAULT_START_BALL_LOCATION_Y:i32 = (WINDOW_HEIGHT / 5) * 4 - DEFAULT_BALL_WIDTH / 2;
|
||||
|
||||
const DEFAULT_MAP_WIDTH:i32 = 40;
|
||||
const DEFAULT_MAP_HEIGHT:i32 = 50;
|
||||
const DEFAULT_TEST_MAP:&[i32] = &[
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0, 0,0,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|
||||
pub struct Map{
|
||||
size:Point,
|
||||
grid:Vec<i32>,
|
||||
}
|
||||
impl Map{
|
||||
fn new() -> Self{
|
||||
let (DEFAULT_MAP,height) = (50,40);
|
||||
Map{
|
||||
size:Point::new(DEFAULT_MAP_WIDTH,DEFAULT_MAP_HEIGHT),
|
||||
//grid:vec![0;width as usize * height as usize],
|
||||
grid:DEFAULT_TEST_MAP.to_vec(),
|
||||
}
|
||||
}
|
||||
fn randomize(&mut self){
|
||||
let mut rng = rand::thread_rng();
|
||||
for x in 0..self.size.x{
|
||||
for y in 0..self.size.y{
|
||||
let index = (x * self.size.y) + y;
|
||||
self.grid[index as usize] = rng.gen_range(0..3);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>,debug:bool){
|
||||
let width = WINDOW_WIDTH / self.size.x;
|
||||
let height = WINDOW_HEIGHT / self.size.y;
|
||||
for x in 0..self.size.x{
|
||||
for y in 0..self.size.y{
|
||||
let rect = Rect::new(x * width,y * height,width as u32,height as u32);
|
||||
let index = (x * height) + y;
|
||||
let tile_color:Color = Color::RGB(0xFF, 0xFF, 0xFF);
|
||||
let tile_bg:Color;
|
||||
match self.grid[index as usize]{
|
||||
0 => {
|
||||
//Ground
|
||||
tile_bg = Color::RGB(0x6A,0x9A,0x1D);
|
||||
}
|
||||
1 =>{
|
||||
//Water
|
||||
tile_bg = Color::RGB(0x59, 0xA5, 0xD8);
|
||||
}
|
||||
2 =>{
|
||||
//Wall
|
||||
tile_bg = Color::RGB(0x17, 0x2A, 0x33);
|
||||
}
|
||||
_ => {
|
||||
//uuuuuuh... whatever I guess lets make it black
|
||||
tile_bg = Color::RGB(0x00, 0x00, 0x00);
|
||||
},
|
||||
}
|
||||
ctx.set_draw_color(tile_bg);
|
||||
ctx.fill_rect(rect).expect("Could not render grid");
|
||||
if debug{
|
||||
ctx.set_draw_color(tile_color);
|
||||
ctx.draw_rect(rect).expect("Could not render grid");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Game{
|
||||
main_ball:Entity,
|
||||
draw_debug:bool,
|
||||
ball_released:bool,
|
||||
map:Map,
|
||||
}
|
||||
impl Game{
|
||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
|
||||
let main_ball_color:Color = Color::RGB(0xFA, 0xFD, 0xF6);
|
||||
let main_ball_outline:Color = Color::RGB(0x17, 0x2A, 0x33);
|
||||
let background_color:Color = Color::RGB(0x6A,0x9A,0x1D);
|
||||
let background_color:Color = Color::RGB(0xFF,0xFF,0xFF);
|
||||
ctx.set_draw_color(background_color);
|
||||
ctx.clear();
|
||||
//map
|
||||
self.map.render(ctx,self.draw_debug);
|
||||
//ball
|
||||
let center:Point = Point::new(self.main_ball.shape.x + (self.main_ball.shape.width() / 2) as i32,self.main_ball.shape.y + (self.main_ball.shape.width() / 2) as i32);
|
||||
if self.draw_debug {
|
||||
ctx.set_draw_color(main_ball_color);
|
||||
@@ -212,6 +335,9 @@ fn is_left_pressed(e: &sdl2::EventPump)-> bool{
|
||||
fn is_right_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::D) || e.keyboard_state().is_scancode_pressed(Scancode::Right)
|
||||
}
|
||||
fn is_random_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::R)
|
||||
}
|
||||
fn is_tab_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::Tab)
|
||||
}
|
||||
@@ -249,6 +375,9 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
|
||||
if is_right_pressed(event_pump){
|
||||
game.right();
|
||||
}
|
||||
if is_random_pressed(event_pump){
|
||||
game.map.randomize();
|
||||
}
|
||||
if is_tab_pressed(event_pump){
|
||||
if tab_state == false{
|
||||
//Keydown
|
||||
@@ -328,6 +457,6 @@ pub fn main() {
|
||||
shape:main_ball_rectangle,
|
||||
velocity:main_ball_velocity
|
||||
};
|
||||
let mut game = Game{main_ball:main_entity,draw_debug:false,ball_released:false};
|
||||
let mut game = Game{main_ball:main_entity,draw_debug:false,ball_released:false,map:Map::new()};
|
||||
main_loop(&mut canvas,&mut event_pump,&mut game);
|
||||
}
|
||||
Reference in New Issue
Block a user