Shabingus two files LETS GOOOOOO OwO

main
Johannes Hendrik Gerard van der Weide 2023-12-09 17:17:31 +01:00
parent af393f6a30
commit 8019e71014
2 changed files with 143 additions and 125 deletions

View File

@ -1,7 +1,9 @@
use bevy::prelude::*;
use player::PlayerPlugin;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
use crate::player::Player;
#[derive(Component, Serialize, Deserialize)]
pub struct Settings {
@ -14,31 +16,14 @@ pub struct Settings {
pub input_sprite_location: Vec2,
}
#[derive(Serialize, Deserialize)]
pub struct PlayerYAML {
pub sprite_path: String,
pub speed: f32,
pub movement: Vec2,
pub direction: Vec2,
pub dash: bool,
pub dash_modifier: f32,
}
#[derive(Component)]
pub struct Player {
pub speed: f32,
pub movement: Vec2,
pub direction: Vec2,
pub dash: bool,
pub dash_modifier: f32,
}
#[derive(Component)]
pub struct InputVisual {
pub location: Vec2,
pub input_direction: Vec2,
}
mod player;
fn main() {
App::new()
.add_plugins(
@ -55,10 +40,10 @@ fn main() {
})
.build(),
)
.add_plugins(PlayerPlugin)
.add_systems(Startup, setup)
.add_systems(Update, get_input)
.add_systems(Update, draw_input)
.add_systems(Update, character_movement)
.run();
}
@ -113,53 +98,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
input_direction: Vec2::new(0.0, 0.0),
},
));
//Default player_data
let mut player_data_yaml: String = "
#These values you can just go edit no problem
sprite_path: blobcat_hertog.png
speed: 75.0
movement: [
0.0,
0.0
]
#These you should leave alone
direction: [
0.0,
0.0
]
dash: false
dash_modifier: 0.3
"
.to_string();
let player_data_file = Path::new("./data/player_data.yml");
if player_data_file.exists() {
player_data_yaml = fs::read_to_string("./data/player_data.yml")
.expect("Error reading data/player_data.yml");
} else {
fs::write("./data/player_data.yml", player_data_yaml.clone())
.expect("Error writing data/player_data.yml");
}
let player_data =
serde_yaml::from_str::<PlayerYAML>(&player_data_yaml).expect("Error serializing to YAML");
commands.spawn((
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(50.0, 50.0)),
..default()
},
texture: asset_server.load(player_data.sprite_path),
..default()
},
//TODO: find out how to use vectors in json so I don't have to use this array fuckery
Player {
speed: player_data.speed,
movement: Vec2::new(player_data.movement.x, player_data.movement.y),
direction: Vec2::new(player_data.direction.x, player_data.direction.y),
dash: player_data.dash,
dash_modifier: player_data.dash_modifier,
},
));
}
//TODO: make it so keycodes can be changed (maby through JSON at first and later make a UI menu to edit said JSON)
@ -212,61 +150,3 @@ fn draw_input(mut visuals: Query<(&mut Transform, &mut InputVisual)>, input: Que
}
}
}
fn character_movement(mut characters: Query<(&mut Transform, &mut Player)>, time: Res<Time>) {
for (mut transform, mut player) in &mut characters {
let dash_modifier: f32 = player.dash_modifier;
let current_movement: Vec2 = player.movement;
if player.dash == false {
if player.direction.y == 1.0 && player.movement.y <= 1.0 {
player.movement.y += player.speed;
}
if player.direction.y == 0.0 && player.movement.y >= 1.0 {
player.movement.y -= player.speed;
}
if player.direction.y == -1.0 && player.movement.y >= -1.0 {
player.movement.y -= player.speed;
}
if player.direction.y == 0.0 && player.movement.y <= -1.0 {
player.movement.y += player.speed;
}
if player.direction.x == 1.0 && player.movement.x <= 1.0 {
player.movement.x += player.speed;
}
if player.direction.x == 0.0 && player.movement.x >= 1.0 {
player.movement.x -= player.speed;
}
if player.direction.x == -1.0 && player.movement.x >= -1.0 {
player.movement.x -= player.speed;
}
if player.direction.x == 0.0 && player.movement.x <= -1.0 {
player.movement.x += player.speed;
}
} else {
if player.direction.y == 1.0 {
player.movement += current_movement * dash_modifier;
}
if player.direction.y == -1.0 {
player.movement -= current_movement * dash_modifier;
}
if player.direction.x == 1.0 {
player.movement = Vec2::new(
player.movement.x - player.movement.y * 0.1,
player.movement.y + player.movement.x * 0.1,
);
}
if player.direction.x == -1.0 {
player.movement = Vec2::new(
player.movement.x + player.movement.y * 0.1,
player.movement.y - player.movement.x * 0.1,
);
}
}
transform.translation.x += player.movement.x * time.delta_seconds();
transform.translation.y += player.movement.y * time.delta_seconds();
}
}

138
src/player.rs Normal file
View File

@ -0,0 +1,138 @@
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_player)
.add_systems(Update, player_movement);
}
}
#[derive(Serialize, Deserialize)]
pub struct PlayerYAML {
pub sprite_path: String,
pub speed: f32,
pub movement: Vec2,
pub direction: Vec2,
pub dash: bool,
pub dash_modifier: f32,
}
#[derive(Component)]
pub struct Player {
pub speed: f32,
pub movement: Vec2,
pub direction: Vec2,
pub dash: bool,
pub dash_modifier: f32,
}
fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
//Default player_data
let mut player_data_yaml: String = "
#These values you can just go edit no problem
sprite_path: blobcat_hertog.png
speed: 75.0
movement: [
0.0,
0.0
]
#These you should leave alone
direction: [
0.0,
0.0
]
dash: false
dash_modifier: 0.3
"
.to_string();
let player_data_file = Path::new("./data/player_data.yml");
if player_data_file.exists() {
player_data_yaml = fs::read_to_string("./data/player_data.yml")
.expect("Error reading data/player_data.yml");
} else {
fs::write("./data/player_data.yml", player_data_yaml.clone())
.expect("Error writing data/player_data.yml");
}
let player_data =
serde_yaml::from_str::<PlayerYAML>(&player_data_yaml).expect("Error serializing to YAML");
commands.spawn((
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(50.0, 50.0)),
..default()
},
texture: asset_server.load(player_data.sprite_path),
..default()
},
//TODO: find out how to use vectors in json so I don't have to use this array fuckery
Player {
speed: player_data.speed,
movement: Vec2::new(player_data.movement.x, player_data.movement.y),
direction: Vec2::new(player_data.direction.x, player_data.direction.y),
dash: player_data.dash,
dash_modifier: player_data.dash_modifier,
},
));
}
fn player_movement(mut characters: Query<(&mut Transform, &mut Player)>, time: Res<Time>) {
for (mut transform, mut player) in &mut characters {
let dash_modifier: f32 = player.dash_modifier;
let current_movement: Vec2 = player.movement;
if player.dash == false {
if player.direction.y == 1.0 && player.movement.y <= 1.0 {
player.movement.y += player.speed;
}
if player.direction.y == 0.0 && player.movement.y >= 1.0 {
player.movement.y -= player.speed;
}
if player.direction.y == -1.0 && player.movement.y >= -1.0 {
player.movement.y -= player.speed;
}
if player.direction.y == 0.0 && player.movement.y <= -1.0 {
player.movement.y += player.speed;
}
if player.direction.x == 1.0 && player.movement.x <= 1.0 {
player.movement.x += player.speed;
}
if player.direction.x == 0.0 && player.movement.x >= 1.0 {
player.movement.x -= player.speed;
}
if player.direction.x == -1.0 && player.movement.x >= -1.0 {
player.movement.x -= player.speed;
}
if player.direction.x == 0.0 && player.movement.x <= -1.0 {
player.movement.x += player.speed;
}
} else {
if player.direction.y == 1.0 {
player.movement += current_movement * dash_modifier;
}
if player.direction.y == -1.0 {
player.movement -= current_movement * dash_modifier;
}
if player.direction.x == 1.0 {
player.movement = Vec2::new(
player.movement.x - player.movement.y * 0.1,
player.movement.y + player.movement.x * 0.1,
);
}
if player.direction.x == -1.0 {
player.movement = Vec2::new(
player.movement.x + player.movement.y * 0.1,
player.movement.y - player.movement.x * 0.1,
);
}
}
transform.translation.x += player.movement.x * time.delta_seconds();
transform.translation.y += player.movement.y * time.delta_seconds();
}
}