monstrous/src/main.rs

141 lines
3.9 KiB
Rust
Raw Normal View History

2022-08-10 20:28:33 +02:00
// Copyright 2022 ModZero.
// SPDX-License-Identifier: AGPL-3.0-or-later
2022-08-10 17:54:15 +02:00
use bevy::{
2022-08-11 05:11:22 +02:00
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
2022-08-10 17:54:15 +02:00
input::mouse::MouseMotion,
prelude::*,
};
2022-05-13 00:36:25 +02:00
use bevy_ecs_tilemap::prelude::*;
2022-12-23 12:48:48 +01:00
use defs::SurfaceDef;
2022-05-13 00:36:25 +02:00
2022-11-01 21:06:09 +01:00
mod defs;
2022-12-23 12:48:48 +01:00
const TERRAINS: [SurfaceDef; 3] = [
SurfaceDef {
label: "Mud",
name: "mud",
description: "Soil saturated with water.",
texture_index: 13,
support: 20.0,
},
SurfaceDef {
label: "Grass",
name: "grass",
description: "Green. Try to touch it.",
texture_index: 14,
support: 40.0,
},
SurfaceDef {
label: "Sand",
name: "sand",
description: "Gets everywhere, ruins vanilla sex fantasies.",
texture_index: 15,
support: 20.0,
},
];
#[derive(Component, Clone)]
struct TileTerrain {
terrain_id: usize,
}
impl TileTerrain {
fn def(&self) -> &SurfaceDef {
&TERRAINS[self.terrain_id]
}
}
2022-08-11 05:11:22 +02:00
fn make_ground_layer(
commands: &mut Commands,
tilemap_size: TilemapSize,
texture_handle: Handle<Image>,
tile_size: TilemapTileSize,
) {
2022-08-10 17:54:15 +02:00
let mut tile_storage = TileStorage::empty(tilemap_size);
2022-12-04 17:12:37 +01:00
let tilemap_entity = commands.spawn_empty().id();
2022-05-13 00:36:25 +02:00
2022-08-11 05:11:22 +02:00
for x in 0..tilemap_size.x {
for y in 0..tilemap_size.y {
2022-08-10 17:54:15 +02:00
let tile_pos = TilePos { x, y };
2022-12-23 12:48:48 +01:00
let tile_terrain = TileTerrain { terrain_id: 1 };
2022-08-10 17:54:15 +02:00
let tile_entity = commands
2022-12-23 12:48:48 +01:00
.spawn((
tile_terrain.clone(),
TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index: TileTextureIndex(tile_terrain.def().texture_index),
..Default::default()
},
))
2022-08-10 17:54:15 +02:00
.id();
2022-11-01 21:45:02 +01:00
tile_storage.set(&tile_pos, tile_entity);
2022-08-10 17:54:15 +02:00
}
}
2022-05-13 00:36:25 +02:00
2022-11-01 21:45:02 +01:00
let grid_size = tile_size.into();
2022-12-04 17:12:37 +01:00
let map_type = TilemapType::default();
2022-11-01 21:45:02 +01:00
2022-12-23 12:48:48 +01:00
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: tilemap_size,
storage: tile_storage.clone(),
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, &map_type, 0.0),
..Default::default()
});
2022-05-13 00:36:25 +02:00
}
2022-08-11 05:11:22 +02:00
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
2022-12-04 17:12:37 +01:00
commands.spawn(Camera2dBundle::default());
2022-08-11 05:11:22 +02:00
let texture_handle = asset_server.load("tileset.png");
let tilemap_size = TilemapSize { x: 320, y: 320 };
let tile_size = TilemapTileSize { x: 32.0, y: 32.0 };
2022-12-23 12:48:48 +01:00
make_ground_layer(&mut commands, tilemap_size, texture_handle, tile_size);
2022-08-11 05:11:22 +02:00
}
2022-05-13 01:13:09 +02:00
fn mouse_motion(
mut motion_evr: EventReader<MouseMotion>,
buttons: Res<Input<MouseButton>>,
mut query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
) {
if buttons.pressed(MouseButton::Middle) {
for ev in motion_evr.iter() {
for (mut transform, mut _ortho) in query.iter_mut() {
let direction = Vec3::new(ev.delta.x, ev.delta.y * -1.0, 0.0);
transform.translation += direction;
}
}
}
}
2022-05-12 23:47:19 +02:00
fn main() {
2022-05-13 00:36:25 +02:00
App::new()
2022-12-23 12:48:48 +01:00
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Monstrous"),
..Default::default()
},
..default()
})
.set(ImagePlugin::default_nearest()),
2022-12-04 17:12:37 +01:00
)
2022-08-10 17:54:15 +02:00
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
2022-05-13 00:36:25 +02:00
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
2022-05-13 01:13:09 +02:00
.add_system(mouse_motion)
2022-05-13 00:36:25 +02:00
.run();
2022-05-12 23:47:19 +02:00
}