Skip to content

Commit

Permalink
Merge branch 'floorless'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmut committed Apr 20, 2024
2 parents 0bfe21a + 623498d commit 24bdb32
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 66 deletions.
Binary file modified assets/image/tileset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions game/src/bin/experiments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bioengineer::external::assets_macroquad::load_tileset;
use bioengineer::external::backends::{drawer_factory, UiBackend};
use bioengineer::external::backends::{drawer_factory, TILESET_PATH, UiBackend};
use bioengineer::external::main_input_macroquad::InputMacroquad as InputSource;
use logic::screen::drawer_trait::DrawerTrait;
use logic::screen::gui::set_skin;
Expand Down Expand Up @@ -34,7 +34,7 @@ fn window_conf() -> Conf {
}

async fn factory() -> (Box<dyn DrawerTrait>, Box<InputSource>) {
let tileset = load_tileset("assets/image/tileset.png");
let tileset = load_tileset(TILESET_PATH);
let drawer_name = std::env::args().last();
let mut drawer = drawer_factory_from_name(drawer_name, tileset.await);
set_skin(drawer.as_mut());
Expand Down
9 changes: 7 additions & 2 deletions game/src/bin/hot_reload_bioengineer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::ffi::{c_char, c_int, c_void, CString};
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
use macroquad::input::{is_key_down, is_key_pressed};
use bioengineer::external::assets_macroquad::load_tileset;

use bioengineer::external::backends::{factory, introduction_factory};
use bioengineer::external::backends::{factory, introduction_factory, TILESET_PATH};
use logic::scene::State;
use logic::world::map::chunk::chunks::cache::print_cache_stats;
use logic::SceneState;
use mq_basics::now;
use mq_basics::{KeyCode, now};

const DEFAULT_WINDOW_WIDTH: i32 = 1365;
const DEFAULT_WINDOW_HEIGHT: i32 = 768;
Expand Down Expand Up @@ -66,6 +68,9 @@ async fn main() -> Result<(), AnyError> {
info!("reloading lib");
(draw_frame, lib_handle) = reload(lib_handle)?;
}
if is_key_pressed(KeyCode::T) {
scene.as_mut().as_mut().unwrap().set_textures(load_tileset(TILESET_PATH).await);
}
sleep_until_next_frame(&mut previous_time).await
}
if let Some(SceneState::Main(main_scene)) = *scene {
Expand Down
5 changes: 4 additions & 1 deletion game/src/external/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use logic::SceneState;
use macroquad::texture::Texture2D;
use std::str::FromStr;

pub const TILESET_PATH: &'static str = "assets/image/tileset.png";

#[derive(Debug, Copy, Clone)]
pub enum UiBackend {
Macroquad,
Expand Down Expand Up @@ -44,6 +46,7 @@ pub async fn factory(args: &CliArgs, textures: Vec<Texture2D>) -> Box<Option<Sce
world,
})))
}

pub async fn introduction_factory(args: &CliArgs) -> Box<Option<SceneState>> {
let drawer = drawer_factory(args.ui, Vec::new());
let input = Box::new(InputMacroquad);
Expand All @@ -55,7 +58,7 @@ pub async fn introduction_factory(args: &CliArgs) -> Box<Option<SceneState>> {
Box::new(Some(SceneState::Introduction(IntroductionSceneState::new(
drawer,
input,
TextureLoader::new_from_image(&["assets/image/tileset.png"]),
TextureLoader::new_from_image(&[TILESET_PATH]),
assets_macroquad::split_tileset,
juquad_functions,
))))
Expand Down
6 changes: 6 additions & 0 deletions logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ impl SceneState {
SceneState::Main(state) => state.screen.drawer.take_textures(),
}
}
pub fn set_textures(&mut self, textures: Vec<Texture2D>) {
match self {
SceneState::Introduction(state) => state.set_textures(textures),
SceneState::Main(state) => state.screen.drawer.set_textures(textures),
}
}
}

pub fn frame(scene_wrapper: &mut Box<Option<SceneState>>) -> State {
Expand Down
3 changes: 3 additions & 0 deletions logic/src/scene/introduction_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ impl IntroductionSceneState {
pub fn take_textures(self) -> Vec<Texture2D> {
self.drawer.unwrap().take_textures()
}
pub fn set_textures(&mut self, textures: Vec<Texture2D>) {
self.drawer.as_mut().unwrap().set_textures(textures)
}
}

impl Scene for IntroductionScene {
Expand Down
3 changes: 2 additions & 1 deletion logic/src/screen/coords/cell_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub fn subtile_to_subcell(
subtile_to_subcell_offset(tile) + offset.cast()
}

/// NOTE we are mixing min_cell and max_cell !!! this is intended
/// NOTE we are mixing min_cell and max_cell !!! this is intended because the top corner is
/// high y (upwards), low x (towards top left), low z (towards top right).
pub fn cell_offset(min_cell: &CellIndex, max_cell: &CellIndex) -> CellIndex {
CellIndex::new(min_cell.x, max_cell.y, min_cell.z)
}
Expand Down
8 changes: 6 additions & 2 deletions logic/src/screen/coords/tile_pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ fn zoom_offset(drawing: &DrawingState, tile_center_x: f32) -> PixelPosition {
pub fn subtile_to_pixel_offset(subtile: SubTilePosition, zoom: f32) -> PixelPosition {
PixelPosition::new(
subtile.x * (assets::PIXELS_PER_TILE_WIDTH as f32 * 0.5 * zoom),
subtile.y * (assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.25 * zoom),
(
subtile.y
- assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.0)
* (assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.25 * zoom),
)
}

pub fn pixel_to_subtile_offset(pixel_diff: PixelPosition, zoom: f32) -> SubTilePosition {
SubTilePosition::new(
pixel_diff.x / (assets::PIXELS_PER_TILE_WIDTH as f32 * 0.5 * zoom),
pixel_diff.y / (assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.25 * zoom),
assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.0 +
pixel_diff.y / (assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.25 * zoom),
)
}

Expand Down
87 changes: 59 additions & 28 deletions logic/src/screen/draw_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@ use crate::screen::coords::cast::Cast;
use crate::screen::coords::cell_pixel::{cell_to_pixel, subcell_center_to_pixel};
use crate::screen::coords::truncate::assert_in_range_0_1;
use crate::screen::drawer_trait::DrawerTrait;
use crate::screen::drawing_state::{DrawingState, SubCellIndex};
use crate::screen::drawing_state::{DrawingState, SubCellIndex, SubTilePosition};
use crate::screen::gui::{FONT_SIZE, TEXT_COLOR};
use crate::screen::main_scene_input::PixelPosition;
use crate::world::fluids::VERTICAL_PRESSURE_DIFFERENCE;
use crate::world::map::cell::{ExtraTextures, TextureIndexTrait};
use crate::world::map::{Cell, CellIndex, TileType};
use crate::world::map::cell::{ExtraTextures, is_networkable, TextureIndexTrait};
use crate::world::map::{Cell, CellIndex, Pressure, TileType};
use crate::world::World;
use mq_basics::Color;
use crate::screen::coords::tile_pixel::subtile_to_pixel_offset;

const SELECTION_COLOR: Color = Color::new(0.7, 0.8, 1.0, 1.0);

pub fn draw_map(drawer: &dyn DrawerTrait, world: &World, drawing: &DrawingState) {
let min_cell = &drawing.min_cell;
let max_cell = &drawing.max_cell;
let fog = grey(0.5, 0.7);
for i_y in min_cell.y..=max_cell.y {
// draw fog on lower levels, without affecting the top level textures
drawer.draw_rectangle(0.0, 0.0, drawer.screen_width(), drawer.screen_height(), fog);
for i_z in min_cell.z..=max_cell.z {
for i_x in min_cell.x..=max_cell.x {
draw_cell(drawer, world, CellIndex::new(i_x, i_y, i_z), drawing);
}
}
}
// for i_z in min_cell.z..=max_cell.z {
// for i_x in min_cell.x..=max_cell.x {
// draw_cell(drawer, world, CellIndex::new(i_x, max_cell.y + 1, i_z), drawing);
// }
// }
}

fn draw_cell(
Expand All @@ -38,38 +47,50 @@ fn draw_cell(
let cell = world.map.get_cell(cell_index);
let tile_type = cell.tile_type;
let texture = choose_texture(cell, &tile_type);
let depth = max_cell.y - cell_index.y;

let pixel = cell_to_pixel(cell_index, drawing, screen_width);
// if drawing.highlighted_cells.len() > 0 {
// println!("selected something");
// }
if drawing.highlighted_cells().contains(&cell_index) {
drawer.draw_colored_texture(texture, pixel.x, pixel.y, drawing.zoom, SELECTION_COLOR);
} else {
let opacity = get_opacity(&cell_index, drawing, min_cell, max_cell);
// let opacity = 1.0; // for debugging
let mut pixel = cell_to_pixel(cell_index, drawing, screen_width);
let level_offset = subtile_to_pixel_offset(SubTilePosition::new(1.0/64.0, -0.5), drawing.zoom);
pixel += level_offset * depth as f32;
pixel = pixel.round();

drawer.draw_transparent_texture(texture, pixel.x, pixel.y, drawing.zoom, opacity);
// let opacity = 1.0; // for debugging
let opacity = get_opacity(&cell_index, drawing, min_cell, max_cell, tile_type, cell.pressure);
let mut color =
if depth < 0 {
if is_networkable(tile_type) {
grey(0.25, 0.125)
} else {
grey(0.0, 0.0)
}
} else {
let fog = 1.0 - 0.2 * depth.abs() as f32;
Color::new(fog, fog, fog, opacity)
};
if drawing.highlighted_cells().contains(&cell_index) {
color.r = SELECTION_COLOR.r;
color.g = SELECTION_COLOR.g;
color.b = SELECTION_COLOR.b;
if tile_type == TileType::Air {
color.a = SELECTION_COLOR.a;
}
}
drawer.draw_colored_texture(texture, pixel.x, pixel.y, drawing.zoom, color);
// draw_pressure_number(drawer, cell_index, screen_width, drawing, max_cell, cell)
// draw_cell_hit_box(drawer, game_state, cell_index);
}

fn grey(lightness: f32, opacity: f32) -> Color {
Color::new(lightness, lightness, lightness, opacity)
}

fn choose_texture<'a>(cell: &'a Cell, tile_type: &'a TileType) -> &'a dyn TextureIndexTrait {
if cell.tile_type == TileType::Air {
if cell.renderable_pressure <= 0 {
tile_type
} else if cell.renderable_pressure <= VERTICAL_PRESSURE_DIFFERENCE {
&ExtraTextures::DirtyWaterSurface
} else {
&ExtraTextures::DirtyWaterWall
}
if cell.renderable_pressure <= 0 {
tile_type
} else if cell.renderable_pressure <= VERTICAL_PRESSURE_DIFFERENCE {
&ExtraTextures::DirtyWaterSurface
} else {
if cell.renderable_pressure <= VERTICAL_PRESSURE_DIFFERENCE {
tile_type
} else {
&ExtraTextures::DirtyWaterWall
}
&ExtraTextures::DirtyWaterWall
}
}

Expand All @@ -78,8 +99,18 @@ fn get_opacity(
drawing: &DrawingState,
min_cell: &CellIndex,
max_cell: &CellIndex,
tile_type: TileType,
pressure: Pressure,
) -> f32 {
get_border_opacity(cell_index, min_cell, max_cell, &drawing.subcell_diff)
if
// cell_index.y == max_cell.y &&
tile_type == TileType::Air
&& pressure == 0
{
0.0
} else {
get_border_opacity(cell_index, min_cell, max_cell, &drawing.subcell_diff)
}
}

#[allow(unused)]
Expand Down Expand Up @@ -166,7 +197,7 @@ fn draw_cell_hit_box(drawer: &dyn DrawerTrait, cell_index: CellIndex, drawing: &

/// use this function before `pixel_to_subcell_center()` for a lifted rhombus hitbox
pub fn hitbox_offset() -> PixelPosition {
PixelPosition::new(0.0, assets::PIXELS_PER_TILE_HEIGHT as f32 * 0.125)
PixelPosition::new(0.0, assets::PIXELS_PER_TILE_HEIGHT as f32 * -0.125)
}

/// use this function before `pixel_to_cell()` for a centered square hitbox
Expand Down
2 changes: 1 addition & 1 deletion logic/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl World {
transformation.apply(&mut cell_copy);
if self.networks.add(pos_to_transform, cell_copy.tile_type) {
*cell = cell_copy;
if ages(cell.tile_type) {
if ages(cell.tile_type) { // TODO: is_alive(). otherwise it doesn't make sense to have aging_tiles and life as separate variables
self.aging_tiles.insert(pos_to_transform);
self.life.insert(pos_to_transform);
} else {
Expand Down
8 changes: 4 additions & 4 deletions logic/src/world/gameplay_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod building_tests {
fn test_build_machine_next_to_ship() {
let mut world = World::new();
let cell = world.map.get_ship_position().unwrap() + CellIndex::new(0, 0, 1);
let from_tile = TileType::FloorDirt;
let from_tile = TileType::Air;
let to_tile = TileType::MachineAirCleaner;
assert_eq!(world.map.get_cell(cell).tile_type, from_tile);

Expand All @@ -128,7 +128,7 @@ mod building_tests {
fn test_build_tree_next_to_ship() {
let mut world = World::new();
let cell = world.map.get_ship_position().unwrap() + CellIndex::new(0, 0, 1);
let from_tile = TileType::FloorDirt;
let from_tile = TileType::Air;
let to_tile = TileType::TreeHealthy;
assert_eq!(world.map.get_cell(cell).tile_type, from_tile);

Expand All @@ -142,7 +142,7 @@ mod building_tests {
let mut world = World::new();
world.game_state.set_advance_every_frame();
let cell = world.map.get_ship_position().unwrap() + CellIndex::new(0, 0, 1);
let from_tile = TileType::FloorDirt;
let from_tile = TileType::Air;
let to_tile = TileType::MachineAirCleaner;
assert_eq!(world.map.get_cell(cell).tile_type, from_tile);

Expand Down Expand Up @@ -196,7 +196,7 @@ mod building_tests {
let mut world = World::new();
world.game_state.set_advance_every_frame();
let cell = world.map.get_ship_position().unwrap() + CellIndex::new(0, 0, 1);
let from_tile = TileType::FloorDirt;
let from_tile = TileType::Air;
let to_tile = TileType::TreeHealthy;
assert_eq!(world.map.get_cell(cell).tile_type, from_tile);

Expand Down
14 changes: 8 additions & 6 deletions logic/src/world/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ fn choose_tile_in_island_map(cell_index: CellIndex, cell: &mut Cell) {
let enlargement_by_deepness = -cell_index.y as f32 / steepness;
let is_land = horizontal_distance_from_center < island_radius + enlargement_by_deepness;
if is_land {
cell.pressure = VERTICAL_PRESSURE_DIFFERENCE; // Hack to make floors and machines quickly floodable
cell.tile_type = if cell_index.y == 1 {
TileType::FloorDirt
} else {
TileType::WallRock
};
// cell.pressure = VERTICAL_PRESSURE_DIFFERENCE; // Hack to make floors and machines quickly floodable
cell.tile_type =
if cell_index.y == 1 {
TileType::Air
} else {
TileType::WallRock
}
;
} else {
cell.tile_type = TileType::Air;
use VERTICAL_PRESSURE_DIFFERENCE as PRESSURE;
Expand Down
Loading

0 comments on commit 24bdb32

Please sign in to comment.