enemy spawner refactoring

This commit is contained in:
artem 2024-02-19 22:07:10 +03:00
parent 8dfa066715
commit be60a4346e
3 changed files with 89 additions and 24 deletions

View File

@ -68,7 +68,7 @@ pub struct Enemy {
impl Enemy { impl Enemy {
fn init(&mut self) { fn init(&mut self) {
self.attack_damage = 100.0; self.attack_damage = 100.0;
self.attack_speed = 0.3; self.attack_speed = 0.2;
} }
fn round(&self, a: f32) -> i32 { fn round(&self, a: f32) -> i32 {

View File

@ -40,48 +40,113 @@ impl EnemySpawn {
pub fn new() -> Self { pub fn new() -> Self {
return EnemySpawn::default(); return EnemySpawn::default();
} }
pub fn spawn_enemy(&self, graph: &mut Graph, resource_manager: &ResourceManager) {
// TODO: add with_build fn attack_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
let mut enemy = Enemy::default(); let idle = resource_manager.request::<Texture>(
enemy.x = 0.0; "assets/data/characters/skeleton/Skeleton_Warrior/Attack_3.png".to_owned(),
enemy.y = -4.0; );
let x = enemy.x; let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters {
let y = enemy.y; width: 512,
let mut material = Material::standard_2d(); height: 128,
frame_width: 128,
frame_height: 128,
first_frame: 0,
last_frame: 6,
column_major: false,
});
idle_animation.set_texture(Some(idle));
idle_animation.set_looping(true);
idle_animation.set_speed(10.);
idle_animation.play();
return idle_animation;
}
fn run_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
let idle = resource_manager.request::<Texture>(
"assets/data/characters/skeleton/Skeleton_Warrior/Run.png".to_owned(),
);
let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters {
width: 1024,
height: 128,
frame_width: 128,
frame_height: 128,
first_frame: 0,
last_frame: 8,
column_major: false,
});
idle_animation.set_texture(Some(idle));
idle_animation.set_looping(true);
idle_animation.set_speed(10.);
idle_animation.play();
return idle_animation;
}
fn die_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
let idle = resource_manager.request::<Texture>(
"assets/data/characters/skeleton/Skeleton_Warrior/Dead.png".to_owned(),
);
let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters {
width: 512,
height: 128,
frame_width: 128,
frame_height: 128,
first_frame: 0,
last_frame: 4,
column_major: false,
});
idle_animation.set_texture(Some(idle));
idle_animation.set_looping(false);
idle_animation.set_speed(10.);
idle_animation.play();
return idle_animation;
}
fn idle_anmation(&self, resource_manager: &ResourceManager) -> SpriteSheetAnimation<fyrox::asset::Resource<Texture>> {
let idle = resource_manager.request::<Texture>( let idle = resource_manager.request::<Texture>(
"assets/data/characters/skeleton/Skeleton_Warrior/Idle.png".to_owned(), "assets/data/characters/skeleton/Skeleton_Warrior/Idle.png".to_owned(),
); );
let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters { let mut idle_animation = SpriteSheetAnimation::new_from_image_parameters(ImageParameters {
width: 896, width: 896,
height: 128, height: 128,
frame_width: 118, frame_width: 128,
frame_height: 128, frame_height: 128,
first_frame: 1, first_frame: 0,
last_frame: 5, last_frame: 7,
column_major: false, column_major: false,
}); });
idle_animation.set_texture(Some(idle.clone())); idle_animation.set_texture(Some(idle));
idle_animation.set_looping(true); idle_animation.set_looping(true);
idle_animation.set_speed(10.); idle_animation.set_speed(10.);
idle_animation.play(); idle_animation.play();
return idle_animation;
}
pub fn spawn_enemy(&self, graph: &mut Graph, resource_manager: &ResourceManager, x: f32) {
// TODO: add with_build
let mut enemy = Enemy::default();
enemy.x = x;
enemy.y = -3.0;
let x = enemy.x;
let y = enemy.y;
let mut material = Material::standard_2d();
enemy.animations = vec![ enemy.animations = vec![
idle_animation.clone(), self.idle_anmation(resource_manager),
idle_animation.clone(), self.run_anmation(resource_manager),
idle_animation.clone(), self.attack_anmation(resource_manager),
idle_animation, self.die_anmation(resource_manager),
]; ];
material material
.set_property( .set_property(
&ImmutableString::new("diffuseTexture"), &ImmutableString::new("diffuseTexture"),
PropertyValue::Sampler { PropertyValue::Sampler {
value: Some(idle.clone()), value: None,
fallback: SamplerFallback::Normal, fallback: SamplerFallback::Normal,
}, },
) )
.unwrap(); .unwrap();
let shape = ColliderShape::Capsule(CapsuleShape { let shape = ColliderShape::Capsule(CapsuleShape {
begin: Vector2::new(0.0, -0.2), begin: Vector2::new(0.0, 0.4),
end: Vector2::new(0.0, 0.1), end: Vector2::new(0.0, 0.0),
radius: 0.3, radius: 0.3,
}); });
let rb_transform = TransformBuilder::new() let rb_transform = TransformBuilder::new()
@ -110,10 +175,10 @@ impl EnemySpawn {
.with_local_transform(rb_transform) .with_local_transform(rb_transform)
.with_script(Script::new(enemy)), .with_script(Script::new(enemy)),
) )
.with_mass(1.0) .with_mass(10.0)
// Turn off gravity for tile // Turn off gravity for tile
.with_gravity_scale(1.) .with_gravity_scale(1.)
.with_lin_damping(2.0) .with_lin_damping(0.0)
// Set tile to be static and not rotate // Set tile to be static and not rotate
.with_rotation_locked(true) .with_rotation_locked(true)
.with_body_type(RigidBodyType::Dynamic) .with_body_type(RigidBodyType::Dynamic)
@ -123,7 +188,7 @@ impl EnemySpawn {
impl ScriptTrait for EnemySpawn { impl ScriptTrait for EnemySpawn {
fn on_init(&mut self, context: &mut ScriptContext) { fn on_init(&mut self, context: &mut ScriptContext) {
let resource_manager: &ResourceManager = &context.resource_manager; let resource_manager: &ResourceManager = &context.resource_manager;
self.spawn_enemy(&mut context.scene.graph, resource_manager); self.spawn_enemy(&mut context.scene.graph, resource_manager, 0.0);
// self.enemy.on_init(context); // self.enemy.on_init(context);
} }
} }

View File

@ -110,6 +110,6 @@ impl Plugin for Game {
let graph: &mut Graph = &mut context.scenes[self.scene].graph; let graph: &mut Graph = &mut context.scenes[self.scene].graph;
let resource_manager: &ResourceManager = &context.resource_manager; let resource_manager: &ResourceManager = &context.resource_manager;
build_map(graph, resource_manager); build_map(graph, resource_manager);
EnemySpawn::new().spawn_enemy(graph, resource_manager); EnemySpawn::new().spawn_enemy(graph, resource_manager, 0.0);
} }
} }