Skip to content

Commit

Permalink
Back to where we were, but now with better tiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn committed May 14, 2021
1 parent 67df940 commit 78548f1
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 231 deletions.
16 changes: 4 additions & 12 deletions src/game/gameplay/camera/movement.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use bevy::{prelude::*, render::camera::{Camera, CameraProjection, OrthographicProjection}};

use crate::game::{camera::{CameraData, CustomOrthographicProjection}, gameplay::character::PlayerSprite};
use crate::game::{camera::{CameraData}, gameplay::character::PlayerSprite};

pub fn movement(
time: Res<Time>,
windows: Res<Windows>,

player_query: Query<&Transform, (With<PlayerSprite>, Without<Camera>)>,
mut camera_query: Query<(
&mut CameraData,
Expand All @@ -17,16 +16,9 @@ pub fn movement(
for player_transform in player_query.iter() {
player_position = player_transform.translation;
}
for (mut camera_data, mut camera, mut camera_transform, mut projection) in camera_query.iter_mut() {
for (_camera_data, mut camera, mut camera_transform, mut projection) in camera_query.iter_mut() {
let camera_z = camera_transform.translation.z;

// camera_transform.translation = camera_transform.translation.truncate().lerp(player_position.truncate(), 0.01).extend(camera_z);

projection.update(
windows.get_primary().unwrap().width(),
windows.get_primary().unwrap().height(),
);
camera.projection_matrix = projection.get_projection_matrix();
camera.depth_calculation = projection.depth_calculation();
camera_transform.translation = camera_transform.translation.truncate().lerp(player_position.truncate(), 0.05).extend(camera_z);
}
}
4 changes: 2 additions & 2 deletions src/game/gameplay/character/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn spawn_player(
.id();
build_basic_character_attributes(&mut commands, character);

let map_player_texture_handle = asset_server.load("textures/player_sprite.png");
let map_player_texture_handle = asset_server.get_handle("textures/player_sprite.png");
let map_player_sprite_material = materials.add(map_player_texture_handle.into());
commands
.spawn_bundle(SpriteBundle {
Expand All @@ -39,7 +39,7 @@ pub fn spawn_player(
.insert(RenderLayers::layer(0));


let battle_player_texture_handle = asset_server.load("textures/characters/huntress/idle.png");
let battle_player_texture_handle = asset_server.get_handle("textures/characters/huntress/idle.png");
let texture_atlas =
TextureAtlas::from_grid(battle_player_texture_handle, Vec2::new(150.0, 150.0), 8, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
Expand Down
42 changes: 24 additions & 18 deletions src/game/gameplay/character/movement.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
use crate::game::map::MapData;
use bevy::prelude::*;

use crate::game::map::{MAP_LAYER, MapData, MapQuery};

use super::PlayerSprite;

pub fn movement(
time: Res<Time>,
map: Res<MapData>,
mut player_query: Query<(&mut PlayerSprite, &mut Transform)>,
map_query: MapQuery,
map_data_query: Query<&MapData>,
) {
if map.road_path.len() > 0 {
for (mut player, mut transform) in player_query.iter_mut() {
let current_road_position = map.road_path[player.current_position];
let current_road_position = Vec2::new(
(current_road_position.0 as f32 * 16.0) + 8.0,
(current_road_position.1 as f32 * 16.0) + 8.0,
);
let mut player_position = transform.translation.truncate();
if let Some((entity, _)) = map_query.get_layer(MAP_LAYER) {
if let Ok(map_data) = map_data_query.get(entity) {
if map_data.road_path.len() > 0 {
for (mut player, mut transform) in player_query.iter_mut() {
let current_road_position = map_data.road_path[player.current_position];
let current_road_position = Vec2::new(
(current_road_position.x as f32 * 16.0) + 8.0,
(current_road_position.y as f32 * 16.0) + 8.0,
);
let mut player_position = transform.translation.truncate();


let direction = (current_road_position - player_position).normalize();
player_position += direction * 100.0 * time.delta_seconds();
let direction = (current_road_position - player_position).normalize();
player_position += direction * 100.0 * time.delta_seconds();

transform.translation = player_position.extend(10.0);
transform.translation = player_position.extend(10.0);

let distance = current_road_position.distance_squared(player_position);
if distance <= 1.0 {
player.current_position += 1;
if player.current_position >= map.road_path.len() {
player.current_position = 0;
let distance = current_road_position.distance_squared(player_position);
if distance <= 1.0 {
player.current_position += 1;
if player.current_position >= map_data.road_path.len() {
player.current_position = 0;
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/gameplay/enemy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn spawn_enemy(
.insert(Transform::from_xyz(position.x, position.y, 11.0))
.insert(Enemy::default())
.with_children(|child_builder| {
let texture_handle: Handle<Texture> = asset_server.load("textures/enemies/skeleton/idle.png");
let texture_handle: Handle<Texture> = asset_server.get_handle("textures/enemies/skeleton/idle.png");
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(150.0, 150.0), 4, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
child_builder
Expand All @@ -46,7 +46,7 @@ pub fn create_battle_enemy(
asset_server: &Res<AssetServer>,
texture_atlases: &mut ResMut<Assets<TextureAtlas>>,
) {
let texture_handle: Handle<Texture> = asset_server.load("textures/enemies/skeleton/idle.png");
let texture_handle: Handle<Texture> = asset_server.get_handle("textures/enemies/skeleton/idle.png");
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(150.0, 150.0), 4, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

Expand Down
7 changes: 4 additions & 3 deletions src/game/gameplay/enemy/spawner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, render::camera::RenderLayers};
use crate::game::GameState;
use super::spawn_enemy;

Expand All @@ -17,15 +17,16 @@ pub fn spawn(
materials: &mut ResMut<Assets<ColorMaterial>>,
position: Vec2,
) {
let texture_handle: Handle<Texture> = asset_server.load("textures/cave_sprite.png");
dbg!(position);
let texture_handle: Handle<Texture> = asset_server.get_handle("textures/cave_sprite.png");
let cave_sprite_material = materials.add(texture_handle.into());
commands
.spawn_bundle(SpriteBundle {
material: cave_sprite_material,
transform: Transform::from_xyz(position.x, position.y, 10.0),
..Default::default()
})
.insert(GameState::MapView)
.insert(RenderLayers::layer(0))
.insert(Spawner {
wait_time: 10.0,
last_time: 0.0,
Expand Down
4 changes: 2 additions & 2 deletions src/game/gameplay/scenes/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn handle_battle_events(
) {
for event in battle_events.iter() {
let texture_handle: Handle<Texture> =
asset_server.load(get_battle_location_texture(event.battle_location));
asset_server.get_handle(get_battle_location_texture(event.battle_location));
let background_sprite = materials.add(texture_handle.into());

// Get character entity..
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn handle_battle_events(
// Accepts a `String` or any type that converts into a `String`, such as `&str`
"Health: ",
TextStyle {
font: asset_server.load("FiraMono-Medium.ttf"),
font: asset_server.get_handle("FiraMono-Medium.ttf"),
font_size: 24.0,
color: Color::WHITE,
},
Expand Down
30 changes: 1 addition & 29 deletions src/game/gameplay/scenes/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::game::{camera::{CameraData, CurrentCamera}};
pub fn spawn(
mut commands: Commands,
mut current_camera: ResMut<CurrentCamera>,
mut tilemap_query: Query<&mut Map>,
) {
let mut ortho = OrthographicCameraBundle::new_2d();
ortho.orthographic_projection.scale = 0.5;
Expand All @@ -15,42 +14,15 @@ pub fn spawn(
.spawn()
.insert_bundle(ortho)
.insert(CameraData::default())
// .insert(RenderLayers::layer(0))
.insert(RenderLayers::layer(0))
.id();

current_camera.camera = Some(camera_entity);

for mut tilemap in tilemap_query.iter_mut() {
// let map_width = tilemap.width().unwrap() as i32;
// let map_height = tilemap.height().unwrap() as i32;
// let half_map_width = map_width / 2;
// let half_map_height = map_height / 2;

// for x in -half_map_width..half_map_width {
// for y in -half_map_height..half_map_height {
// tilemap.spawn_chunk((x, y)).unwrap();
// }
// }
}
}

pub fn destroy(
mut commands: Commands,
mut current_camera: ResMut<CurrentCamera>,
mut tilemap_query: Query<&mut Map>,
) {
commands.entity(current_camera.camera.take().unwrap()).despawn_recursive();

for mut tilemap in tilemap_query.iter_mut() {
// let map_width = tilemap.width().unwrap() as i32;
// let map_height = tilemap.height().unwrap() as i32;
// let half_map_width = map_width / 2;
// let half_map_height = map_height / 2;

// for x in -half_map_width..half_map_width {
// for y in -half_map_height..half_map_height {
// tilemap.despawn_chunk((x, y)).unwrap();
// }
// }
}
}
12 changes: 4 additions & 8 deletions src/game/loading.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use bevy::{asset::LoadState, prelude::*};
use super::map::TilemapAtlasHandles;
use super::{map::get_has_map_assets, GameState};
use super::{GameState, LoadingHandles};

// A system to determin if we have finished loading and should change states.
pub fn loading(
asset_server: Res<AssetServer>,
mut game_state: ResMut<State<GameState>>,
tilemap_atlas_handles: Res<TilemapAtlasHandles>,
textures: Res<Assets<Texture>>,
loading_handles: Res<LoadingHandles>,
) {
asset_server.load::<Font, &'static str>("FiraMono-Medium.ttf");

if asset_server.get_group_load_state(textures.iter().map(|(handle_id, _)| handle_id)) == LoadState::Loaded &&
get_has_map_assets(asset_server, tilemap_atlas_handles)
dbg!(loading_handles.0.len());
if asset_server.get_group_load_state(loading_handles.0.iter().map(|handle_id| handle_id.id)) == LoadState::Loaded
{
game_state.set(GameState::SpawnMap).unwrap();
}
Expand Down
Loading

0 comments on commit 78548f1

Please sign in to comment.