client: can click on city
This commit is contained in:
parent
c12f6dc506
commit
088f4f91e9
14 changed files with 647 additions and 3178 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
.idea/
|
||||
target/
|
||||
dist/
|
||||
|
|
|
|||
3002
client/Cargo.lock
generated
3002
client/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,10 +1,11 @@
|
|||
[package]
|
||||
name = "clash"
|
||||
name = "client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "^0.9"
|
||||
bevy_ecs_tilemap = "^0.9"
|
||||
macroquad = "0.3"
|
||||
hex2d = "1.1.0"
|
||||
server = { path = "../server", version = "0.1.0" }
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
use bevy::{input::Input, math::Vec3, prelude::*, render::camera::Camera};
|
||||
|
||||
// A simple camera system for moving and zooming the camera.
|
||||
#[allow(dead_code)]
|
||||
pub fn movement(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
mut query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
|
||||
) {
|
||||
for (mut transform, mut ortho) in query.iter_mut() {
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
if keyboard_input.pressed(KeyCode::A) {
|
||||
direction -= Vec3::new(1.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::D) {
|
||||
direction += Vec3::new(1.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::W) {
|
||||
direction += Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::S) {
|
||||
direction -= Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::Z) {
|
||||
ortho.scale += 0.1;
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::X) {
|
||||
ortho.scale -= 0.1;
|
||||
}
|
||||
|
||||
if ortho.scale < 0.5 {
|
||||
ortho.scale = 0.5;
|
||||
}
|
||||
|
||||
let z = transform.translation.z;
|
||||
transform.translation += time.delta_seconds() * direction * 500.;
|
||||
// Important! We need to restore the Z values when moving the camera around.
|
||||
// Bevy has a specific camera setup and this can mess with how our layers are shown.
|
||||
transform.translation.z = z;
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
pub mod camera;
|
||||
|
|
@ -1,136 +1,52 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_ecs_tilemap::prelude::*;
|
||||
mod map;
|
||||
mod ui;
|
||||
|
||||
mod helpers;
|
||||
use crate::map::pixel_to_coordinate;
|
||||
use macroquad::prelude::*;
|
||||
use server::city::City;
|
||||
use server::game::Game;
|
||||
use server::hexagon::Position;
|
||||
|
||||
// Side length of a colored quadrant (in "number of tiles").
|
||||
const QUADRANT_SIDE_LENGTH: u32 = 80;
|
||||
#[macroquad::main("Clash")]
|
||||
async fn main() {
|
||||
let mut status: String = "".to_string();
|
||||
|
||||
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
let mut game = Game::new(1, "a".repeat(32));
|
||||
game.players[0]
|
||||
.cities
|
||||
.push(City::new(0, Position::from_offset("A1")));
|
||||
game.players[0]
|
||||
.cities
|
||||
.push(City::new(0, Position::from_offset("C2")));
|
||||
|
||||
let texture_handle: Handle<Image> = asset_server.load("flat_hex_tiles.png");
|
||||
loop {
|
||||
clear_background(RED);
|
||||
|
||||
// In total, there will be `(QUADRANT_SIDE_LENGTH * 2) * (QUADRANT_SIDE_LENGTH * 2)` tiles.
|
||||
let map_size = TilemapSize {
|
||||
x: QUADRANT_SIDE_LENGTH * 2,
|
||||
y: QUADRANT_SIDE_LENGTH * 2,
|
||||
};
|
||||
let quadrant_size = TilemapSize {
|
||||
x: QUADRANT_SIDE_LENGTH,
|
||||
y: QUADRANT_SIDE_LENGTH,
|
||||
};
|
||||
|
||||
let mut tile_storage = TileStorage::empty(map_size);
|
||||
let tilemap_entity = commands.spawn_empty().id();
|
||||
let tilemap_id = TilemapId(tilemap_entity);
|
||||
|
||||
fill_tilemap_rect(
|
||||
TileTextureIndex(0),
|
||||
TilePos { x: 0, y: 0 },
|
||||
quadrant_size,
|
||||
tilemap_id,
|
||||
&mut commands,
|
||||
&mut tile_storage,
|
||||
);
|
||||
|
||||
fill_tilemap_rect(
|
||||
TileTextureIndex(1),
|
||||
TilePos {
|
||||
x: QUADRANT_SIDE_LENGTH,
|
||||
y: 0,
|
||||
},
|
||||
quadrant_size,
|
||||
tilemap_id,
|
||||
&mut commands,
|
||||
&mut tile_storage,
|
||||
);
|
||||
|
||||
fill_tilemap_rect(
|
||||
TileTextureIndex(2),
|
||||
TilePos {
|
||||
x: 0,
|
||||
y: QUADRANT_SIDE_LENGTH,
|
||||
},
|
||||
quadrant_size,
|
||||
tilemap_id,
|
||||
&mut commands,
|
||||
&mut tile_storage,
|
||||
);
|
||||
|
||||
fill_tilemap_rect(
|
||||
TileTextureIndex(3),
|
||||
TilePos {
|
||||
x: QUADRANT_SIDE_LENGTH,
|
||||
y: QUADRANT_SIDE_LENGTH,
|
||||
},
|
||||
quadrant_size,
|
||||
tilemap_id,
|
||||
&mut commands,
|
||||
&mut tile_storage,
|
||||
);
|
||||
|
||||
let tile_size = TilemapTileSize { x: 17.0, y: 15.0 };
|
||||
let grid_size = TilemapGridSize { x: 17.0, y: 15.0 };
|
||||
let map_type = TilemapType::Hexagon(HexCoordSystem::Column);
|
||||
|
||||
commands.entity(tilemap_entity).insert(TilemapBundle {
|
||||
grid_size,
|
||||
size: map_size,
|
||||
storage: tile_storage,
|
||||
texture: TilemapTexture::Single(texture_handle),
|
||||
tile_size,
|
||||
map_type,
|
||||
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
fn swap_mesh_type(
|
||||
mut query: Query<(
|
||||
&mut Transform,
|
||||
&TilemapSize,
|
||||
&TilemapGridSize,
|
||||
&mut TilemapType,
|
||||
)>,
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
) {
|
||||
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||
for (mut transform, map_size, grid_size, mut map_type) in query.iter_mut() {
|
||||
match *map_type {
|
||||
TilemapType::Hexagon(HexCoordSystem::Column) => {
|
||||
*map_type = TilemapType::Hexagon(HexCoordSystem::ColumnEven);
|
||||
}
|
||||
TilemapType::Hexagon(HexCoordSystem::ColumnEven) => {
|
||||
*map_type = TilemapType::Hexagon(HexCoordSystem::ColumnOdd);
|
||||
}
|
||||
TilemapType::Hexagon(HexCoordSystem::ColumnOdd) => {
|
||||
*map_type = TilemapType::Hexagon(HexCoordSystem::Column);
|
||||
}
|
||||
_ => {}
|
||||
for p in game.players.iter() {
|
||||
for city in p.cities.iter() {
|
||||
map::draw_hex(&city.position);
|
||||
}
|
||||
|
||||
*transform = get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0);
|
||||
}
|
||||
|
||||
if is_mouse_button_pressed(MouseButton::Left) {
|
||||
let (x, y) = mouse_position();
|
||||
|
||||
let c = pixel_to_coordinate(x, y);
|
||||
|
||||
status = "".to_string();
|
||||
|
||||
for p in game.players.iter() {
|
||||
for city in p.cities.iter() {
|
||||
let pos = &city.position;
|
||||
if c == pos.coordinate() {
|
||||
let n = pos.name();
|
||||
status = format!("clicked city {n}")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
draw_text(&status, 20.0, 20.0, 30.0, DARKGRAY);
|
||||
|
||||
next_frame().await
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
window: WindowDescriptor {
|
||||
title: String::from("Hexagon Column Example"),
|
||||
..Default::default()
|
||||
},
|
||||
..default()
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),
|
||||
)
|
||||
.add_plugin(TilemapPlugin)
|
||||
.add_startup_system(startup)
|
||||
.add_system(helpers::camera::movement)
|
||||
.add_system(swap_mesh_type)
|
||||
.run();
|
||||
}
|
||||
|
|
|
|||
27
client/src/map.rs
Normal file
27
client/src/map.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use crate::ui::Point;
|
||||
use hex2d::Coordinate;
|
||||
use hex2d::Spacing;
|
||||
use macroquad::prelude::*;
|
||||
use server::hexagon::Position;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
const SIZE: f32 = 80.0;
|
||||
const SPACING: Spacing = Spacing::FlatTop(SIZE);
|
||||
|
||||
pub fn center(pos: &Position) -> Point {
|
||||
let p = pos.coordinate().to_pixel(SPACING);
|
||||
Point { x: p.0, y: p.1 }
|
||||
}
|
||||
|
||||
pub(crate) fn draw_hex(p: &Position) {
|
||||
let c = center(p).to_screen();
|
||||
draw_hexagon(c.x, c.y, SIZE, 2.0, false, DARKGRAY, BLUE);
|
||||
draw_text(&p.to_string(), c.x, c.y, 40.0, DARKGRAY);
|
||||
}
|
||||
|
||||
pub fn pixel_to_coordinate(x: f32, y: f32) -> Coordinate {
|
||||
let p = Point::new(x, y).to_game();
|
||||
|
||||
let c = Coordinate::from_pixel(p.x, p.y, SPACING);
|
||||
c
|
||||
}
|
||||
26
client/src/ui.rs
Normal file
26
client/src/ui.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
const TOP_BORDER: f32 = 130.0;
|
||||
const LEFT_BORDER: f32 = 90.0;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Point {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn new(x: f32, y: f32) -> Point {
|
||||
Point { x, y }
|
||||
}
|
||||
|
||||
pub fn to_screen(self) -> Point {
|
||||
let x = self.x + LEFT_BORDER;
|
||||
let y = TOP_BORDER - self.y;
|
||||
Point { x, y }
|
||||
}
|
||||
|
||||
pub fn to_game(self) -> Point {
|
||||
let x = self.x - LEFT_BORDER;
|
||||
let y = TOP_BORDER - self.y;
|
||||
Point { x, y }
|
||||
}
|
||||
}
|
||||
199
server/Cargo.lock
generated
199
server/Cargo.lock
generated
|
|
@ -2,21 +2,24 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.8"
|
||||
|
|
@ -24,8 +27,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasi",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hex2d"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "043b896b6e8a9d66c52171ff404f122d8c000f90cfc8078b3cf6fcdbfd3ae08b"
|
||||
dependencies = [
|
||||
"num",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -34,12 +50,112 @@ version = "1.0.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.139"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
|
|
@ -131,6 +247,17 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"hex2d",
|
||||
"rand",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.107"
|
||||
|
|
@ -153,3 +280,57 @@ name = "wasi"
|
|||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
|
||||
|
|
|
|||
|
|
@ -1,11 +1,17 @@
|
|||
[package]
|
||||
name = "game"
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
name = "server"
|
||||
crate-type = ["lib"]
|
||||
|
||||
[dependencies]
|
||||
hex2d = "1.1.0"
|
||||
rand = "0.8.5"
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
serde_json = "1.0.91"
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
|
|
|
|||
|
|
@ -409,7 +409,7 @@ mod tests {
|
|||
let old = 0;
|
||||
let new = 1;
|
||||
|
||||
let position = Position::new(0, 0, 0);
|
||||
let position = Position::new(0, 0);
|
||||
game.players[old]
|
||||
.cities
|
||||
.push(City::new(old, position.clone()));
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ use super::custom_actions::CustomActionType::*;
|
|||
use crate::{civilization::Civilization, leader::Leader};
|
||||
|
||||
pub fn get_civilizations() -> Vec<Civilization> {
|
||||
vec![]
|
||||
vec![
|
||||
Civilization::new("test", vec![], vec![])
|
||||
]
|
||||
}
|
||||
|
||||
pub fn get_civilization_by_name(name: &str) -> Option<Civilization> {
|
||||
|
|
|
|||
|
|
@ -1,22 +1,49 @@
|
|||
use hex2d::Coordinate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
|
||||
pub struct Position {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub z: i32,
|
||||
pub q: i32,
|
||||
pub r: i32,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn new(x: i32, y: i32, z: i32) -> Self {
|
||||
Self { x, y, z }
|
||||
pub fn new(q: i32, r: i32) -> Self {
|
||||
Self { q, r }
|
||||
}
|
||||
|
||||
// using Odd Q - https://www.redblobgames.com/grids/hexagons/#coordinates-offset
|
||||
pub fn from_offset(s: &str) -> Position {
|
||||
let mut chars = s.chars();
|
||||
let row = chars.next().expect("string is emtpy") as u32 - 'A' as u32;
|
||||
let col = s.get(1..).expect("string is too short").parse::<u32>().expect("not a number");
|
||||
let q = (col - 1) as i32;
|
||||
let r = (row as i32) - (q - (q % 2)) / 2;
|
||||
Position::new (q, r)
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
let c = self.q;
|
||||
let r = self.r + (self.q - (self.q % 2)) / 2;
|
||||
let row = char::from_u32(('A' as u32) + r as u32).unwrap();
|
||||
let col = c + 1;
|
||||
format!("{row}{col}")
|
||||
}
|
||||
|
||||
pub fn coordinate(&self) -> Coordinate {
|
||||
// x == r
|
||||
// y == q
|
||||
Coordinate::new(self.r, self.q)
|
||||
}
|
||||
|
||||
pub fn distance(&self, other: &Self) -> u32 {
|
||||
let distance_x = self.x.abs_diff(other.x);
|
||||
let distance_y = self.y.abs_diff(other.y);
|
||||
let distance_z = self.z.abs_diff(other.z);
|
||||
distance_x + distance_y + distance_z
|
||||
self.coordinate().distance(other.coordinate()) as u32
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Position {
|
||||
fn to_string(&self) -> String {
|
||||
self.name()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,3 +55,15 @@ pub enum Landmark {
|
|||
Unusable,
|
||||
Water,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::hexagon::Position;
|
||||
|
||||
#[test]
|
||||
fn convert_position() {
|
||||
let position = Position::from_offset("A1");
|
||||
assert_eq!(Position::new(0, 0), position);
|
||||
assert_eq!("A1", position.name());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
mod ability_initializer;
|
||||
mod advance;
|
||||
mod army;
|
||||
mod city;
|
||||
pub mod city;
|
||||
mod civilization;
|
||||
mod content;
|
||||
mod events;
|
||||
mod game;
|
||||
pub mod game;
|
||||
mod game_api;
|
||||
mod hexagon;
|
||||
pub mod hexagon;
|
||||
mod leader;
|
||||
mod player;
|
||||
mod player_events;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue