end of day 1
This commit is contained in:
parent
8e2cf6b0a6
commit
f6b16a16ed
73
Cargo.lock
generated
73
Cargo.lock
generated
|
@ -346,19 +346,6 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_ecs_tilemap"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9ae9433efc108cc26600b214e6c114d7d3df650d414e2505e1e5726c3ca1938"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bytemuck",
|
||||
"log",
|
||||
"morton-encoding",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_gilrs"
|
||||
version = "0.6.0"
|
||||
|
@ -1579,7 +1566,7 @@ dependencies = [
|
|||
"byteorder",
|
||||
"color_quant",
|
||||
"num-iter",
|
||||
"num-rational 0.3.2",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
"png",
|
||||
"scoped_threadpool",
|
||||
|
@ -1913,16 +1900,6 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "morton-encoding"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f66c953d92a578cd98a4598021e3b473520d214665917eb51dba49dc227936c8"
|
||||
dependencies = [
|
||||
"num",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "naga"
|
||||
version = "0.8.5"
|
||||
|
@ -2084,41 +2061,6 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational 0.2.4",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-derive"
|
||||
version = "0.3.3"
|
||||
|
@ -2151,18 +2093,6 @@ dependencies = [
|
|||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.3.2"
|
||||
|
@ -2677,7 +2607,6 @@ name = "souler-power"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy_ecs_tilemap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -7,4 +7,3 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
bevy = "0.6"
|
||||
bevy_ecs_tilemap = "0.5"
|
||||
|
|
127
src/main.rs
127
src/main.rs
|
@ -1,15 +1,33 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_ecs_tilemap::prelude::*;
|
||||
use bevy::sprite::collide_aabb::*;
|
||||
|
||||
mod texture;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Player;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Background;
|
||||
|
||||
const PLAYER_SPEED: f32 = 5.0;
|
||||
|
||||
struct Moveable {
|
||||
down: bool,
|
||||
up: bool,
|
||||
left: bool,
|
||||
right: bool,
|
||||
}
|
||||
|
||||
static mut MOVEABLE: Moveable = Moveable {
|
||||
down: true,
|
||||
up: true,
|
||||
left: true,
|
||||
right: true,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
|
||||
.add_startup_system(setup)
|
||||
.add_startup_system(spawn_player)
|
||||
.add_system(movement)
|
||||
|
@ -18,11 +36,17 @@ fn main() {
|
|||
.add_system(bevy::input::system::exit_on_esc_system)
|
||||
.add_system(texture::set_texture_filters_to_nearest)
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugin(TilemapPlugin)
|
||||
.run()
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let background_image: Handle<Image> = asset_server.load("Untitled.png");
|
||||
commands
|
||||
.spawn_bundle(SpriteBundle {
|
||||
texture: background_image.into(),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Background);
|
||||
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
||||
}
|
||||
fn spawn_player(
|
||||
|
@ -42,8 +66,8 @@ fn spawn_player(
|
|||
},
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Player)
|
||||
.insert(Transform::from_xyz(0.0, 0.0, 1.0))
|
||||
.insert(Player)
|
||||
.insert(Timer::from_seconds(0.1, true));
|
||||
}
|
||||
|
||||
|
@ -79,51 +103,25 @@ fn movement(
|
|||
mut player_positions: Query<&mut Transform, With<Player>>,
|
||||
) {
|
||||
for mut transform in player_positions.iter_mut() {
|
||||
if keyboard_input.pressed(KeyCode::A) {
|
||||
let move_to = move_towards(
|
||||
transform.translation.truncate(),
|
||||
Vec2::new(
|
||||
transform.translation.x - PLAYER_SPEED,
|
||||
transform.translation.y,
|
||||
),
|
||||
1.0,
|
||||
);
|
||||
transform.translation = move_to.extend(1.0);
|
||||
transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::D) {
|
||||
let move_to = move_towards(
|
||||
transform.translation.truncate(),
|
||||
Vec2::new(
|
||||
transform.translation.x + PLAYER_SPEED,
|
||||
transform.translation.y,
|
||||
),
|
||||
1.0,
|
||||
);
|
||||
transform.translation = move_to.extend(1.0);
|
||||
transform.rotation = Quat::default();
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::S) {
|
||||
let move_to = move_towards(
|
||||
transform.translation.truncate(),
|
||||
Vec2::new(
|
||||
transform.translation.x,
|
||||
transform.translation.y - PLAYER_SPEED,
|
||||
),
|
||||
1.0,
|
||||
);
|
||||
transform.translation = move_to.extend(1.0);
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::W) {
|
||||
let move_to = move_towards(
|
||||
transform.translation.truncate(),
|
||||
Vec2::new(
|
||||
transform.translation.x,
|
||||
transform.translation.y + PLAYER_SPEED,
|
||||
),
|
||||
1.0,
|
||||
);
|
||||
transform.translation = move_to.extend(1.0);
|
||||
unsafe {
|
||||
if keyboard_input.pressed(KeyCode::A) && MOVEABLE.left {
|
||||
println!("({}, {})", transform.translation.x, transform.translation.y);
|
||||
transform.translation.x -= PLAYER_SPEED;
|
||||
transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::D) && MOVEABLE.right {
|
||||
println!("({}, {})", transform.translation.x, transform.translation.y);
|
||||
transform.translation.x += PLAYER_SPEED;
|
||||
transform.rotation = Quat::default();
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::S) && MOVEABLE.down {
|
||||
println!("({}, {})", transform.translation.x, transform.translation.y);
|
||||
transform.translation.y -= PLAYER_SPEED;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::W) && MOVEABLE.up {
|
||||
println!("({}, {})", transform.translation.x, transform.translation.y);
|
||||
transform.translation.y += PLAYER_SPEED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +132,29 @@ fn follow_player(
|
|||
) {
|
||||
let mut camera = camera_positions.single_mut();
|
||||
let player = player_positions.single();
|
||||
if distance_to(camera.translation.truncate(), player.translation.truncate()) > 100.0 {
|
||||
unsafe {
|
||||
if player.translation.x > 650.0 {
|
||||
MOVEABLE.right = false;
|
||||
} else {
|
||||
MOVEABLE.right = true;
|
||||
}
|
||||
if player.translation.x < -650.0 {
|
||||
MOVEABLE.left = false;
|
||||
} else {
|
||||
MOVEABLE.left = true;
|
||||
}
|
||||
if player.translation.y > 370.0 {
|
||||
MOVEABLE.up = false;
|
||||
} else {
|
||||
MOVEABLE.up = true;
|
||||
}
|
||||
if player.translation.y < -370.0 {
|
||||
MOVEABLE.down = false;
|
||||
} else {
|
||||
MOVEABLE.down = true;
|
||||
}
|
||||
}
|
||||
if distance_to(camera.translation.truncate(), player.translation.truncate()) > 300.0 {
|
||||
camera.translation = player.translation;
|
||||
}
|
||||
}
|
||||
|
@ -144,10 +164,3 @@ fn distance_to(point1: Vec2, point2: Vec2) -> f32 {
|
|||
+ ((point1.y - point2.y) * (point1.y - point2.y));
|
||||
return squared.sqrt();
|
||||
}
|
||||
|
||||
fn move_towards(a: Vec2, b: Vec2, distance: f32) -> Vec2 {
|
||||
let vector = Vec2::new(b.x - a.x, b.y - a.y);
|
||||
let length: f32 = (vector.x * vector.x + vector.y * vector.y).sqrt();
|
||||
let unit_vector = Vec2::new(vector.x / length, vector.y / length);
|
||||
return Vec2::new(a.x + unit_vector.x * 2.0, a.y + unit_vector.y * distance);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue