Added dash as a mode (badly I'll fix it later)

main
Johannes Hendrik Gerard van der Weide 2023-12-02 20:34:07 +01:00
parent 253e1654d8
commit 32ed8b2bf8
3 changed files with 94 additions and 34 deletions

BIN
assets/neocat.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

@ -1,8 +1,9 @@
#These values you can just go edit no problem
speed: 0.3
sprite_path: blobcat_hertog.png
speed: 75.0
movement: [
10.0,
0.0,
0.0
]
#These you should leave alone
@ -11,4 +12,5 @@
0.0
]
dash: false
dash_modifier: 0.3

View File

@ -11,12 +11,23 @@ pub struct Settings {
pub right: String,
}
#[derive(Component, Serialize, Deserialize)]
#[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 main() {
@ -50,21 +61,26 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
down: S
left: A
right: D
".to_string();
"
.to_string();
let settings_file = Path::new("./data/settings.yml");
if settings_file.exists() {
settings_yaml = fs::read_to_string("./data/settings.yml").expect("Error reading data/settings.yml");
settings_yaml =
fs::read_to_string("./data/settings.yml").expect("Error reading data/settings.yml");
} else {
fs::write("./data/settings.yml", settings_yaml.clone()).expect("Error writing data/settings.yml");
fs::write("./data/settings.yml", settings_yaml.clone())
.expect("Error writing data/settings.yml");
}
let settings_data = serde_yaml::from_str::<Settings>(&settings_yaml).expect("Error serializing to YAML");
//Default player_data
let settings_data =
serde_yaml::from_str::<Settings>(&settings_yaml).expect("Error serializing to YAML");
//Default player_data
let mut player_data_yaml: String = "
#These values you can just go edit no problem
speed: 0.3
sprite_path: blobcat_hertog.png
speed: 75.0
movement: [
10.0,
0.0,
0.0
]
#These you should leave alone
@ -73,21 +89,26 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
0.0
]
dash: false
".to_string();
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");
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");
fs::write("./data/player_data.yml", player_data_yaml.clone())
.expect("Error writing data/player_data.yml");
}
let player_data = serde_yaml::from_str::<Player>(&player_data_yaml).expect("Error serializing to YAML");
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("blobcat_hertog.png"),
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
@ -96,6 +117,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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,
},
));
}
@ -130,31 +152,67 @@ fn get_input(mut input_recievers: Query<&mut Player>, input: Res<Input<KeyCode>>
if input.just_released(KeyCode::A) {
player.direction.x += 1.0;
}
if input.just_pressed(KeyCode::Space) {
player.dash = true;
}
if input.just_released(KeyCode::Space) {
player.dash = false;
}
}
}
fn character_movement(mut characters: Query<(&mut Transform, &mut Player)>, time: Res<Time>) {
for (mut transform, mut player) in &mut characters {
let current_speed: f32 = player.speed;
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 += current_movement * current_speed;
}
if player.direction.y == -1.0 {
player.movement -= current_movement * current_speed;
}
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,
);
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,
);
}
}
//TODO: make locking the player within bounds not ass and use the screensize instead of magic numbers