Commit cd16b88f authored by Simon Goller's avatar Simon Goller
Browse files

Add camera system

The camera system sets the transform of the canvas.  The pixel coordinate of
the camera position is centered in the canvas.
parent 75059964
...@@ -77,9 +77,7 @@ pub fn draw_nodes( ...@@ -77,9 +77,7 @@ pub fn draw_nodes(
rendering.stroke(); rendering.stroke();
let time = clock.wall_time(); let time = clock.wall_time();
crate::console_log!("Test");
let node_color = resources::Color::with_hex_string("#6800e7").unwrap(); let node_color = resources::Color::with_hex_string("#6800e7").unwrap();
crate::console_log!("Node color: {:?}", node_color);
if let Some(amount_completed) = node.amount_completed(time) { if let Some(amount_completed) = node.amount_completed(time) {
rendering.begin_path(); rendering.begin_path();
rendering.set_stroke_style(&node_color); rendering.set_stroke_style(&node_color);
......
...@@ -4,20 +4,27 @@ use wasm_bindgen::JsCast; ...@@ -4,20 +4,27 @@ use wasm_bindgen::JsCast;
use web_sys; use web_sys;
fn get_hex_color_format(value: &str) -> u32 { fn get_hex_color_format(value: &str) -> u32 {
let valid_characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', let valid_characters = [
'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B',
'A', 'B', 'C', 'D', 'E', 'F']; 'C', 'D', 'E', 'F',
];
let (hash_tag, value) = value.split_at(1); let (hash_tag, value) = value.split_at(1);
if hash_tag == "#" { if hash_tag == "#" {
value.chars().map(|character| if valid_characters.contains(&character) { 1 } else { 0 }) value
.fold(0, |acc, x| acc * x + 1) .chars()
.map(|character| {
if valid_characters.contains(&character) {
1
} else {
0
}
})
.fold(0, |acc, x| acc * x + 1)
} else { } else {
0 0
} }
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct Color { pub struct Color {
pub red: f64, pub red: f64,
...@@ -46,38 +53,54 @@ impl Color { ...@@ -46,38 +53,54 @@ impl Color {
pub fn with_hex_string(hex_string: &str) -> Option<Self> { pub fn with_hex_string(hex_string: &str) -> Option<Self> {
match get_hex_color_format(hex_string) { match get_hex_color_format(hex_string) {
3 => { 3 => {
let number = u16::from_str_radix(hex_string.get(1..4).unwrap(), 16).unwrap() ; let number = u16::from_str_radix(hex_string.get(1..4).unwrap(), 16).unwrap();
let r = (number & 0xF00) >> 8; let r = (number & 0xF00) >> 8;
let g = (number & 0x0F0) >> 4; let g = (number & 0x0F0) >> 4;
let b = number & 0x00F; let b = number & 0x00F;
Some(Self::with_rgb(r as f64 / 15.0, g as f64 / 15.0, b as f64 / 15.0)) Some(Self::with_rgb(
}, r as f64 / 15.0,
g as f64 / 15.0,
b as f64 / 15.0,
))
}
4 => { 4 => {
let number = u16::from_str_radix(hex_string.get(1..5).unwrap(), 16).unwrap() ; let number = u16::from_str_radix(hex_string.get(1..5).unwrap(), 16).unwrap();
let r = (number & 0xF000) >> 12; let r = (number & 0xF000) >> 12;
let g = (number & 0x0F00) >> 8; let g = (number & 0x0F00) >> 8;
let b = (number & 0x00F0) >> 4; let b = (number & 0x00F0) >> 4;
let a = number & 0x000F; let a = number & 0x000F;
Some(Self::with_rgba(r as f64 / 15.0, g as f64 / 15.0, b as f64 / 15.0, a as f64 / 15.0)) Some(Self::with_rgba(
}, r as f64 / 15.0,
g as f64 / 15.0,
b as f64 / 15.0,
a as f64 / 15.0,
))
}
6 => { 6 => {
let number = u32::from_str_radix(hex_string.get(1..7).unwrap(), 16).unwrap(); let number = u32::from_str_radix(hex_string.get(1..7).unwrap(), 16).unwrap();
let r = (number & 0xFF0000) >> 16; let r = (number & 0xFF0000) >> 16;
let g = (number & 0x00FF00) >> 8; let g = (number & 0x00FF00) >> 8;
let b = number & 0x0000FF; let b = number & 0x0000FF;
Some(Self::with_rgb(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0)) Some(Self::with_rgb(
}, r as f64 / 255.0,
g as f64 / 255.0,
b as f64 / 255.0,
))
}
8 => { 8 => {
let number = u32::from_str_radix(hex_string.get(1..9).unwrap(), 16).unwrap(); let number = u32::from_str_radix(hex_string.get(1..9).unwrap(), 16).unwrap();
let r = (number & 0xFF000000) >> 24; let r = (number & 0xFF000000) >> 24;
let g = (number & 0x00FF0000) >> 16; let g = (number & 0x00FF0000) >> 16;
let b = (number & 0x0000FF00) >> 8; let b = (number & 0x0000FF00) >> 8;
let a = number & 0x000000FF; let a = number & 0x000000FF;
Some(Self::with_rgba(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0, a as f64 / 255.0)) Some(Self::with_rgba(
}, r as f64 / 255.0,
_ => { g as f64 / 255.0,
None b as f64 / 255.0,
a as f64 / 255.0,
))
} }
_ => None,
} }
} }
...@@ -180,4 +203,8 @@ impl Rendering { ...@@ -180,4 +203,8 @@ impl Rendering {
pub fn line_to(&self, x: f64, y: f64) { pub fn line_to(&self, x: f64, y: f64) {
self.ctx.line_to(x, y); self.ctx.line_to(x, y);
} }
pub fn set_transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.ctx.set_transform(a, b, c, d, e, f).unwrap();
}
} }
...@@ -26,6 +26,7 @@ impl InGameState { ...@@ -26,6 +26,7 @@ impl InGameState {
let schedule = legion::Schedule::builder() let schedule = legion::Schedule::builder()
.add_system(systems::moving_system()) .add_system(systems::moving_system())
.add_thread_local(systems::camera_system())
.add_thread_local(systems::draw_level_layer_system(background)) .add_thread_local(systems::draw_level_layer_system(background))
.add_thread_local(systems::draw_level_layer_system(foreground)) .add_thread_local(systems::draw_level_layer_system(foreground))
.build(); .build();
......
use crate::resources;
#[legion::system]
pub fn camera(
#[resource] rendering: &resources::Rendering,
#[resource] camera: &resources::Camera,
) {
let pos = -camera.position + camera.size / 2.0;
rendering.set_transform(1.0, 0.0, 0.0, 1.0, pos.x, pos.y);
}
...@@ -4,10 +4,8 @@ use crate::resources; ...@@ -4,10 +4,8 @@ use crate::resources;
pub fn draw_level_layer( pub fn draw_level_layer(
#[state] handle: &resources::ImageHandle, #[state] handle: &resources::ImageHandle,
#[resource] rendering: &resources::Rendering, #[resource] rendering: &resources::Rendering,
#[resource] camera: &resources::Camera,
) { ) {
let pos = -camera.position + camera.size / 2.0; match rendering.draw_image(*handle, 0.0, 0.0) {
match rendering.draw_image(*handle, pos.x, pos.y) {
Ok(_) => {} Ok(_) => {}
Err(err) => crate::console_log!("Error drawing level layer: {}", err), Err(err) => crate::console_log!("Error drawing level layer: {}", err),
} }
......
mod camera;
mod level; mod level;
mod moving; mod moving;
pub use camera::camera_system;
pub use level::draw_level_layer_system; pub use level::draw_level_layer_system;
pub use moving::moving_system; pub use moving::moving_system;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment