Skip to content

Commit

Permalink
feat:add shake delta
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jun 9, 2024
1 parent a7e47d3 commit 1552dbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion gdrust/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


20 changes: 14 additions & 6 deletions gdrust/src/fight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Fight {
base: Base<Control>,
shake_times: i32,
origin_offset: Vector2,
shake_delta: f32,
}

#[godot_api]
Expand All @@ -18,13 +19,14 @@ impl IControl for Fight {
base,
shake_times: 0,
origin_offset: Vector2::ZERO,
shake_delta: 0.0,
}
}

fn ready(&mut self) {
let mut enchanted_sword = self.base().get_node_as::<Sprite2D>("Enchanted_Sword");
enchanted_sword.call("fightStart".into(), &[]);
self.shake(3.0);
self.shake(3.0, 1.0);
}
}

Expand All @@ -33,11 +35,12 @@ impl Fight {
/// 晃动屏幕
/// duration: 持续时间
#[func]
fn shake(&mut self, duration: f64) {
fn shake(&mut self, duration: f64, delta: f32) {
// 根据每次等待时间和总时间算出需要震动多少次
let (camera, mut timer) = self.get_shake_timer();
let wait_time = timer.get_wait_time();
let amount = (duration / wait_time) as i32;
self.shake_delta = f32::max(self.shake_delta, delta);
if self.shake_times != 0 {
self.shake_times += amount;
return;
Expand All @@ -47,9 +50,13 @@ impl Fight {
timer.start();
}

fn get_camera(&self) -> Gd<Camera2D> {
self.base().get_node_as::<Camera2D>("Camera2D")
}

fn get_shake_timer(&self) -> (Gd<Camera2D>, Gd<Timer>) {
let camera = self.base().get_node_as::<Camera2D>("Camera2D");
let timer = camera.get_node_as::<Timer>("ShakeTimer");
let camera = self.get_camera();
let timer = camera.get_node_as::<Timer>("Shake");
(camera, timer)
}

Expand All @@ -61,13 +68,14 @@ impl Fight {
// 设回原来的位置
camera.set_offset(self.origin_offset);
timer.stop();
self.shake_delta = 0.0;
return;
}
camera.set_offset(
self.origin_offset
+ Vector2::new(
rand::thread_rng().gen_range(-1.0..1.0),
rand::thread_rng().gen_range(-1.0..1.0),
rand::thread_rng().gen_range(-self.shake_delta..=self.shake_delta),
rand::thread_rng().gen_range(-self.shake_delta..=self.shake_delta),
),
);
}
Expand Down

0 comments on commit 1552dbb

Please sign in to comment.