Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split game_state into smaller structs that render
- Loading branch information
Showing
12 changed files
with
258 additions
and
155 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ edition = "2018" | |
[dependencies] | ||
quicksilver = "*" | ||
immi = "*" | ||
noise = "0.5.1" | ||
noise = "0.5.1" | ||
rand = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::structs::field::Field; | ||
use crate::structs::cell::{ | ||
CellFeature, | ||
CellType | ||
}; | ||
|
||
use crate::structs::camera_work::CameraWork; | ||
|
||
use quicksilver::{ | ||
Result, | ||
graphics::{Color}, | ||
lifecycle::{Window} | ||
}; | ||
pub struct Grid<'a> { | ||
cam : &'a CameraWork, | ||
grid : &'a Field, | ||
} | ||
impl<'a> Grid<'a> { | ||
pub fn new(cam : &'a CameraWork, grid : &'a Field) -> Self { | ||
Grid { | ||
cam, | ||
grid, | ||
} | ||
} | ||
pub fn render(&self, window : &mut Window) -> Result<()> { | ||
/*let cell_size = self.cam.calc_size(); | ||
let height = self.cam.height / cell_size; | ||
let width = self.cam.width / cell_size; | ||
let start_x = CameraWork::calc_start(self.cam.cam.x, width); | ||
let start_y = CameraWork::calc_start(self.cam.cam.y, height); | ||
let end_x = 1 + start_x + width as isize; | ||
let end_y = 1 + start_y + height as isize; | ||
*/ | ||
let (start,end) = self.cam.get_outer_cell_points(); | ||
let part = self.grid.get_part(start,end); | ||
part.iter().enumerate().for_each( | ||
|v| { | ||
let color = | ||
match &v.1.feature { | ||
Some(feature) => match feature { | ||
CellFeature::Wall => Color::INDIGO | ||
}, | ||
None => match v.1.cell_type { | ||
CellType::Water => Color::BLUE, | ||
CellType::Ground => Color::ORANGE, | ||
CellType::Grass => Color::GREEN, | ||
CellType::Stone => Color::from_rgba(50,50,50,1.0) | ||
} | ||
}; | ||
self.cam.draw_full_square_on_grid(&v.1.loc, color, window); | ||
} | ||
); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! This contains structures that render specific parts of the game | ||
pub mod grid; | ||
pub mod mouse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::structs::camera_work::CameraWork; | ||
use crate::funcs::math::sub_from_highest; | ||
use crate::structs::point::{Point,PointWithItem}; | ||
use crate::structs::cell::CellFeature; | ||
use crate::structs::field::Field; | ||
|
||
use quicksilver::prelude::MouseButton; | ||
use quicksilver::{ | ||
Result, | ||
graphics::{Color}, | ||
lifecycle::Window | ||
}; | ||
pub struct Mouse<'a> { | ||
cam : &'a CameraWork, | ||
clicked : &'a mut Option<Point>, | ||
grid : &'a mut Field | ||
} | ||
impl<'a> Mouse<'a> { | ||
pub fn new(cam : &'a CameraWork, clicked : &'a mut Option<Point>, grid : &'a mut Field ) -> Self { | ||
Mouse { | ||
cam, | ||
clicked, | ||
grid | ||
} | ||
} | ||
pub fn render(&mut self, window : &mut Window) -> Result<()> { | ||
let mouse = window.mouse(); | ||
let key = mouse[MouseButton::Left]; | ||
if let Some(grid_pos) = self.cam.screen_to_grid(mouse.pos()) { | ||
self.cam.draw_full_square_on_grid(&grid_pos, Color::WHITE, window); | ||
if let Some(click_point) = &self.clicked { | ||
let dif_x = sub_from_highest(grid_pos.x, click_point.x); | ||
let dif_y = sub_from_highest(grid_pos.y, click_point.y); | ||
let line = | ||
if dif_x > dif_y { | ||
let point = if click_point.x < grid_pos.x { | ||
Point {x : click_point.x,y: click_point.y} | ||
} else { | ||
Point {x : grid_pos.x,y: click_point.y} | ||
}; | ||
point.make_horizontal_line(dif_x) | ||
} else { | ||
let point = if click_point.y < grid_pos.y { | ||
Point {x : click_point.x,y: click_point.y} | ||
} else { | ||
Point {x : click_point.x,y: grid_pos.y} | ||
}; | ||
point.make_vertical_line(dif_y) | ||
}; | ||
line.iter().for_each(|v| self.cam.draw_full_square_on_grid(v, Color::WHITE, window)); | ||
if !key.is_down() { | ||
let line :Vec<PointWithItem<CellFeature>> = line.iter().map(|v|v.add_item(CellFeature::Wall)).collect(); | ||
self.grid.add_feature_to_cells(line); | ||
*self.clicked = None; | ||
} | ||
} else if key.is_down() { | ||
*self.clicked = Some(grid_pos); | ||
} else { | ||
*self.clicked = None; | ||
} | ||
} else { | ||
*self.clicked = None; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,178 +1,67 @@ | ||
use crate::structs::cell::CellFeature; | ||
use crate::structs::point::PointWithItem; | ||
|
||
use crate::structs::point::Point; | ||
use crate::funcs::math::sub_from_highest; | ||
use quicksilver::prelude::MouseButton; | ||
use crate::funcs::math::sub_save; | ||
use crate::funcs::controls::check_multiple; | ||
use quicksilver::prelude::Key; | ||
use quicksilver::prelude::Background::Col; | ||
|
||
use crate::structs::field::Field; | ||
use crate::structs::cell::CellType; | ||
use crate::help_states::grid::Grid; | ||
use crate::help_states::mouse::Mouse; | ||
use crate::structs::camera_work::CameraWork; | ||
use quicksilver::{ | ||
Result, | ||
graphics::{Color}, | ||
lifecycle::{State, Window}, | ||
geom::{Rectangle} | ||
}; | ||
pub struct GameState { | ||
grid : Field, | ||
cam : Point, | ||
scroll : usize, | ||
cam : CameraWork, | ||
clicked : Option<(Point)> | ||
} | ||
impl State for GameState { | ||
fn new() -> Result<Self> { | ||
Ok(Self { | ||
grid : Field::new(101,81,1032), | ||
cam : (101/2 + 1,81/2 +1).into(), | ||
scroll : 100, | ||
clicked : None | ||
clicked : None, | ||
cam : CameraWork { | ||
cam : (101/2 + 1,81/2 +1).into(), | ||
scroll : 100, | ||
width : 800, | ||
height : 600 | ||
} | ||
}) | ||
} | ||
fn update(&mut self, window : &mut Window) -> Result<()> { | ||
let board = window.keyboard(); | ||
|
||
if check_multiple(board,&[Key::Left,Key::A]) { | ||
self.cam.x = sub_save(self.cam.x,1); | ||
self.cam.cam.x = sub_save(self.cam.cam.x,1); | ||
} | ||
if check_multiple(board,&[Key::Right,Key::D]) { | ||
self.cam.x += 1; | ||
self.cam.cam.x += 1; | ||
} | ||
if check_multiple(board,&[Key::Up,Key::W]) { | ||
self.cam.y = sub_save(self.cam.y,1); | ||
self.cam.cam.y = sub_save(self.cam.cam.y,1); | ||
} | ||
if check_multiple(board,&[Key::Down,Key::S]) { | ||
self.cam.y += 1; | ||
self.cam.cam.y += 1; | ||
} | ||
let scroll = window.mouse().wheel().y as isize; | ||
if scroll > self.scroll as isize { | ||
self.scroll = 0 | ||
if scroll > self.cam.scroll as isize { | ||
self.cam.scroll = 0 | ||
} else { | ||
let scroll = (self.scroll as isize - scroll) as usize; | ||
self.scroll = scroll; | ||
let scroll = (self.cam.scroll as isize - scroll) as usize; | ||
self.cam.scroll = scroll; | ||
}; | ||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self, window: &mut Window) -> Result<()> { | ||
window.clear(Color::WHITE)?; | ||
self.draw_grid(window)?; | ||
self.draw_mouse(window)?; | ||
Grid::new(&self.cam,&self.grid).render(window)?; | ||
Mouse::new(&self.cam,&mut self.clicked,&mut self.grid).render(window)?; | ||
Ok(()) | ||
} | ||
} | ||
impl GameState { | ||
fn calc_size (&self) -> usize { | ||
self.scroll / 5 | ||
} | ||
fn grid_to_screen(&self, loc : &Point) -> (f32,f32) { | ||
let cell_size = self.calc_size() as f32; | ||
let width = 800. / cell_size; | ||
let len = 600. / cell_size; | ||
let x = (loc.x as f32 - (self.cam.x as f32 - width as f32 / 2.)) * cell_size as f32; | ||
let y = (loc.y as f32 - (self.cam.y as f32 - len as f32 / 2.)) * cell_size as f32; | ||
(x,y) | ||
} | ||
fn screen_to_grid(&self, loc : quicksilver::geom::Vector) -> Option<Point> { | ||
let cell_size = self.calc_size() as f32; | ||
let x = loc.x / cell_size; | ||
let x = x + (self.cam.x as f32 - (800.0 / cell_size as f32) /2.); | ||
let y = loc.y / cell_size; | ||
let y = y + (self.cam.y as f32 - (600.0 / cell_size as f32) / 2.); | ||
if x <0. || y < 0. { | ||
None | ||
} else { | ||
Some((x as usize, y as usize).into()) | ||
} | ||
|
||
} | ||
fn draw_mouse(&mut self, window : &mut Window) -> Result<()> { | ||
let cell_size = self.calc_size(); | ||
let cell_sizef = cell_size as f32; | ||
let mouse = window.mouse(); | ||
let key = mouse[MouseButton::Left]; | ||
if let Some(grid_pos) = self.screen_to_grid(mouse.pos()) { | ||
let screen_pos = self.grid_to_screen(&grid_pos); | ||
window.draw(&Rectangle::new(screen_pos, (cell_sizef, cell_sizef)), Col(Color::WHITE) ); | ||
if let Some(click_point) = &self.clicked { | ||
let dif_x = sub_from_highest(grid_pos.x, click_point.x); | ||
let dif_y = sub_from_highest(grid_pos.y, click_point.y); | ||
let line = | ||
if dif_x > dif_y { | ||
let point = if click_point.x < grid_pos.x { | ||
Point {x : click_point.x,y: click_point.y} | ||
} else { | ||
Point {x : grid_pos.x,y: click_point.y} | ||
}; | ||
point.make_horizontal_line(dif_x) | ||
} else { | ||
let point = if click_point.y < grid_pos.y { | ||
Point {x : click_point.x,y: click_point.y} | ||
} else { | ||
Point {x : click_point.x,y: grid_pos.y} | ||
}; | ||
point.make_vertical_line(dif_y) | ||
}; | ||
line.iter().for_each(|v| window.draw(&Rectangle::new(self.grid_to_screen(v),(cell_sizef,cell_sizef)),Col(Color::WHITE))); | ||
if !key.is_down() { | ||
let line :Vec<PointWithItem<CellFeature>> = line.iter().map(|v|v.add_item(CellFeature::Wall)).collect(); | ||
self.grid.add_feature_to_cells(line); | ||
self.clicked = None; | ||
} | ||
} else if key.is_down() { | ||
self.clicked = Some(grid_pos); | ||
} else { | ||
self.clicked = None; | ||
} | ||
} else { | ||
self.clicked = None; | ||
} | ||
|
||
Ok(()) | ||
} | ||
fn draw_grid(&self, window : &mut Window) -> Result<()> { | ||
let cell_size = self.calc_size(); | ||
let height = 600 / cell_size; | ||
let width = 800 / cell_size; | ||
let start_x = Self::calc_start(self.cam.x, width); | ||
let start_y = Self::calc_start(self.cam.y, height); | ||
let end_x = 1 + start_x + width as isize; | ||
let end_y = 1 + start_y + height as isize; | ||
let part = self.grid.get_part(start_x, start_y, end_x + 1, end_y); | ||
part.iter().enumerate().for_each( | ||
|v| { | ||
let color = Col( | ||
match &v.1.feature { | ||
Some(feature) => match feature { | ||
CellFeature::Wall => Color::INDIGO | ||
}, | ||
None => match v.1.cell_type { | ||
CellType::Water => Color::BLUE, | ||
CellType::Ground => Color::ORANGE, | ||
CellType::Grass => Color::GREEN, | ||
CellType::Stone => Color::from_rgba(50,50,50,1.0) | ||
} | ||
} | ||
); | ||
let x = (v.1.x - (self.cam.x as isize - width as isize / 2)) * cell_size as isize; | ||
let y = (v.1.y - (self.cam.y as isize - height as isize / 2)) * cell_size as isize; | ||
window.draw(&Rectangle::new((x as f32 , y as f32), (cell_size as f32,cell_size as f32)), color); | ||
} | ||
); | ||
Ok(()) | ||
} | ||
fn calc_start(cam : usize, line_size : usize) -> isize { | ||
let halved = line_size / 2; | ||
if cam < halved || cam == 1 { | ||
0 | ||
} else { | ||
let calced = cam - halved; | ||
if calced <= 1 { | ||
0 | ||
} else { | ||
calced as isize - 1 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.