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(
rendering.stroke();
let time = clock.wall_time();
crate::console_log!("Test");
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) {
rendering.begin_path();
rendering.set_stroke_style(&node_color);
......
......@@ -4,20 +4,27 @@ use wasm_bindgen::JsCast;
use web_sys;
fn get_hex_color_format(value: &str) -> u32 {
let valid_characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f',
'A', 'B', 'C', 'D', 'E', 'F'];
let valid_characters = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B',
'C', 'D', 'E', 'F',
];
let (hash_tag, value) = value.split_at(1);
if hash_tag == "#" {
value.chars().map(|character| if valid_characters.contains(&character) { 1 } else { 0 })
value
.chars()
.map(|character| {
if valid_characters.contains(&character) {
1
} else {
0
}
})
.fold(0, |acc, x| acc * x + 1)
} else {
0
}
}
#[derive(Copy, Clone, Debug)]
pub struct Color {
pub red: f64,
......@@ -46,38 +53,54 @@ impl Color {
pub fn with_hex_string(hex_string: &str) -> Option<Self> {
match get_hex_color_format(hex_string) {
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 g = (number & 0x0F0) >> 4;
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 => {
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 g = (number & 0x0F00) >> 8;
let b = (number & 0x00F0) >> 4;
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 => {
let number = u32::from_str_radix(hex_string.get(1..7).unwrap(), 16).unwrap();
let r = (number & 0xFF0000) >> 16;
let g = (number & 0x00FF00) >> 8;
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 => {
let number = u32::from_str_radix(hex_string.get(1..9).unwrap(), 16).unwrap();
let r = (number & 0xFF000000) >> 24;
let g = (number & 0x00FF0000) >> 16;
let b = (number & 0x0000FF00) >> 8;
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))
},
_ => {
None
Some(Self::with_rgba(
r as f64 / 255.0,
g as f64 / 255.0,
b as f64 / 255.0,
a as f64 / 255.0,
))
}
_ => None,
}
}
......@@ -180,4 +203,8 @@ impl Rendering {
pub fn line_to(&self, x: f64, y: f64) {
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 {
let schedule = legion::Schedule::builder()
.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(foreground))
.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;
pub fn draw_level_layer(
#[state] handle: &resources::ImageHandle,
#[resource] rendering: &resources::Rendering,
#[resource] camera: &resources::Camera,
) {
let pos = -camera.position + camera.size / 2.0;
match rendering.draw_image(*handle, pos.x, pos.y) {
match rendering.draw_image(*handle, 0.0, 0.0) {
Ok(_) => {}
Err(err) => crate::console_log!("Error drawing level layer: {}", err),
}
......
mod camera;
mod level;
mod moving;
pub use camera::camera_system;
pub use level::draw_level_layer_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