feat: add score board and life board
This commit is contained in:
parent
54fa1b222d
commit
e169456958
4 changed files with 71 additions and 25 deletions
|
@ -7,41 +7,29 @@ use FontDirection::{LeftRight, TopDown};
|
||||||
fn main() {
|
fn main() {
|
||||||
let context = Context::create();
|
let context = Context::create();
|
||||||
context.save();
|
context.save();
|
||||||
|
|
||||||
context.translate(70., 50.);
|
context.translate(70., 50.);
|
||||||
// context.scale(8.);
|
|
||||||
|
|
||||||
let font = AsteroidFont::new();
|
let font = AsteroidFont::new();
|
||||||
let line = font.get_text("Hey there!".to_string(), 12., LeftRight);
|
let line = font.get_text("Hey there!".to_string(), 12., LeftRight);
|
||||||
for char in line.iter() {
|
context.draw_line_strings(&line);
|
||||||
context.draw_line_string(char);
|
|
||||||
}
|
|
||||||
context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]);
|
context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]);
|
||||||
|
|
||||||
context.translate(0.0, 18.);
|
context.translate(0.0, 18.);
|
||||||
let line = font.get_text("How are you?".to_string(), 22., LeftRight);
|
let line = font.get_text("How are you?".to_string(), 22., LeftRight);
|
||||||
for char in line.iter() {
|
context.draw_line_strings(&line);
|
||||||
context.draw_line_string(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.translate(0.0, 28.);
|
context.translate(0.0, 28.);
|
||||||
let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight);
|
let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight);
|
||||||
for char in line.iter() {
|
context.draw_line_strings(&line);
|
||||||
context.draw_line_string(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.translate(0.0, 56.);
|
context.translate(0.0, 56.);
|
||||||
let line = font.get_text("0123456789".to_string(), 50., LeftRight);
|
let line = font.get_text("0123456789".to_string(), 50., LeftRight);
|
||||||
for char in line.iter() {
|
context.draw_line_strings(&line);
|
||||||
context.draw_line_string(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.restore();
|
context.restore();
|
||||||
context.translate(30., 50.);
|
context.translate(30., 50.);
|
||||||
let line = font.get_text("0123456789".to_string(), 30., TopDown);
|
let line = font.get_text("0123456789".to_string(), 30., TopDown);
|
||||||
for char in line.iter() {
|
context.draw_line_strings(&line);
|
||||||
context.draw_line_string(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.render();
|
context.render();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ use geo::algorithm::rotate::Rotate;
|
||||||
use geo::prelude::Translate;
|
use geo::prelude::Translate;
|
||||||
use geo::rotate::RotatePoint;
|
use geo::rotate::RotatePoint;
|
||||||
use geo::LineString;
|
use geo::LineString;
|
||||||
use geo::{Coordinate, MultiPolygon, Point, Polygon};
|
use geo::{polygon, Coordinate, MultiPolygon, Point, Polygon};
|
||||||
use geo_clipper::Clipper;
|
use geo_clipper::Clipper;
|
||||||
use polygon_art::MultiPolygonExt;
|
|
||||||
use polygon_art::{load_multipolygon_from_svg_include, Context};
|
use polygon_art::{load_multipolygon_from_svg_include, Context};
|
||||||
|
use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt};
|
||||||
use rand::prelude::StdRng;
|
use rand::prelude::StdRng;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
@ -65,9 +65,11 @@ fn main() {
|
||||||
include_str!("../../pool/asteroids/ship3.svg"),
|
include_str!("../../pool/asteroids/ship3.svg"),
|
||||||
];
|
];
|
||||||
let chosen_ship = rng.gen_range(0..3);
|
let chosen_ship = rng.gen_range(0..3);
|
||||||
let ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship])
|
let raw_ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship])
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.rotate(90.) // rotate to scale ship with width
|
// rotate to scale ship with width
|
||||||
|
.rotate(90.);
|
||||||
|
let ship = raw_ship
|
||||||
.scale_to_width(ship_length)
|
.scale_to_width(ship_length)
|
||||||
.translate_center()
|
.translate_center()
|
||||||
.rotate(rotation as f64);
|
.rotate(rotation as f64);
|
||||||
|
@ -76,9 +78,50 @@ fn main() {
|
||||||
context.draw_multipolygon(&ship);
|
context.draw_multipolygon(&ship);
|
||||||
context.restore();
|
context.restore();
|
||||||
|
|
||||||
// drawing and clipping
|
// draw score board
|
||||||
|
let font = AsteroidFont::new();
|
||||||
|
let random_seed_string = context.seed.to_string();
|
||||||
|
let font_size = 20.;
|
||||||
|
let (x_font_size, y_font_size) = font.get_text_scale_factor(font_size);
|
||||||
|
let score_board_letters = random_seed_string.len();
|
||||||
let window = context.frame(frame_offset, frame_offset, frame_offset, frame_offset);
|
let window = context.frame(frame_offset, frame_offset, frame_offset, frame_offset);
|
||||||
context.draw_polygon(&window);
|
let window_height = frame_offset + y_font_size;
|
||||||
|
let window_width = frame_offset + score_board_letters as f64 * x_font_size;
|
||||||
|
let score_board: Polygon<f64> = polygon![(x:0., y:0.), (x: window_width, y:0.), (x: window_width, y: window_height), (x: 0., y: window_height)];
|
||||||
|
let score_board_lines = font.get_text(random_seed_string, font_size, FontDirection::LeftRight);
|
||||||
|
context.save();
|
||||||
|
context.translate(frame_offset, frame_offset);
|
||||||
|
context.draw_line_strings(&score_board_lines);
|
||||||
|
context.restore();
|
||||||
|
|
||||||
|
// draw life board
|
||||||
|
let life = rng.gen_range(2..7) as f64;
|
||||||
|
let life_board: Polygon<f64> = polygon![
|
||||||
|
(x:width, y:0.),
|
||||||
|
(x: width - frame_offset - life * x_font_size - 3., y:0.),
|
||||||
|
(x: width - frame_offset - life * x_font_size - 3., y:frame_offset + y_font_size + 3.),
|
||||||
|
(x: width , y:frame_offset + y_font_size + 3.)
|
||||||
|
];
|
||||||
|
// todo translate_top
|
||||||
|
// todo translate_left
|
||||||
|
let life_ship = raw_ship
|
||||||
|
.scale_to_width(font_size)
|
||||||
|
.rotate(-90.)
|
||||||
|
.translate_center_y();
|
||||||
|
for l in 1..(1 + life as i32) {
|
||||||
|
context.save();
|
||||||
|
context.translate(
|
||||||
|
width - frame_offset - (x_font_size * l as f64) as f64,
|
||||||
|
frame_offset + y_font_size / 2.,
|
||||||
|
);
|
||||||
|
context.draw_multipolygon(&life_ship);
|
||||||
|
context.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw window
|
||||||
|
let window = window.difference(&score_board, 100.);
|
||||||
|
let window = window.difference(&life_board, 100.);
|
||||||
|
context.draw_multipolygon(&window);
|
||||||
|
|
||||||
for asteroid in asteroids.iter() {
|
for asteroid in asteroids.iter() {
|
||||||
context.draw_multipolygon(&asteroid.intersection(&window, 100.));
|
context.draw_multipolygon(&asteroid.intersection(&window, 100.));
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub struct Context {
|
||||||
pub width: f64,
|
pub width: f64,
|
||||||
pub line_size: f64,
|
pub line_size: f64,
|
||||||
pub pseudo_random_number_generator: Box<StdRng>,
|
pub pseudo_random_number_generator: Box<StdRng>,
|
||||||
|
pub seed: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
|
@ -64,6 +65,12 @@ impl Context {
|
||||||
context.stroke();
|
context.stroke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn draw_line_strings(&self, line_strings: &Vec<LineString<f64>>) {
|
||||||
|
for lines in line_strings {
|
||||||
|
self.draw_line_string(&lines);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// restore state which was saved before by the context
|
/// restore state which was saved before by the context
|
||||||
pub fn restore(&self) {
|
pub fn restore(&self) {
|
||||||
self.render_container.context().restore();
|
self.render_container.context().restore();
|
||||||
|
@ -171,6 +178,7 @@ impl Context {
|
||||||
line_size,
|
line_size,
|
||||||
pseudo_random_number_generator: Box::new(rng),
|
pseudo_random_number_generator: Box::new(rng),
|
||||||
render_container,
|
render_container,
|
||||||
|
seed: random_seed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/font.rs
11
src/font.rs
|
@ -101,6 +101,12 @@ impl AsteroidFont {
|
||||||
polygons
|
polygons
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// size of space (per letter)
|
||||||
|
pub fn get_text_scale_factor(&self, size: f64) -> (f64, f64) {
|
||||||
|
let scale_factor = size / 12.;
|
||||||
|
(10.2 * scale_factor, 14.2 * scale_factor)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_text(
|
pub fn get_text(
|
||||||
&self,
|
&self,
|
||||||
text: String,
|
text: String,
|
||||||
|
@ -111,13 +117,14 @@ impl AsteroidFont {
|
||||||
let mut count = 0.;
|
let mut count = 0.;
|
||||||
for char in text.chars() {
|
for char in text.chars() {
|
||||||
let letter = self.get_letter(&char, size);
|
let letter = self.get_letter(&char, size);
|
||||||
|
let (x_translate_factor, y_translate_factor) = self.get_text_scale_factor(size);
|
||||||
for character in letter.iter() {
|
for character in letter.iter() {
|
||||||
match direction {
|
match direction {
|
||||||
FontDirection::LeftRight => {
|
FontDirection::LeftRight => {
|
||||||
letters.push(character.translate(count * 10.0 * size / 12., 0.))
|
letters.push(character.translate(count * x_translate_factor, 0.))
|
||||||
}
|
}
|
||||||
FontDirection::TopDown => {
|
FontDirection::TopDown => {
|
||||||
letters.push(character.translate(0., count * 14.0 * size / 12.))
|
letters.push(character.translate(0., count * y_translate_factor))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue