-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
156 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use derive::gen_debug; | ||
use godot::{ | ||
builtin::Vector2, | ||
classes::{Camera2D, ICamera2D}, | ||
engine::Timer, | ||
log::godot_print, | ||
obj::{Base, Gd, WithBaseField}, | ||
register::{godot_api, GodotClass}, | ||
}; | ||
use rand::Rng; | ||
|
||
use crate::debug_check; | ||
|
||
#[derive(GodotClass)] | ||
#[class(base = Camera2D)] | ||
pub struct ScreenEffects { | ||
shake_times: i32, | ||
shake_delta: f32, | ||
origin_offset: Vector2, | ||
base: Base<Camera2D>, | ||
} | ||
|
||
#[godot_api] | ||
impl ICamera2D for ScreenEffects { | ||
fn init(base: Base<Camera2D>) -> Self { | ||
Self { | ||
shake_times: 0, | ||
shake_delta: 0.0, | ||
origin_offset: Vector2::ZERO, | ||
base, | ||
} | ||
} | ||
|
||
fn ready(&mut self) { | ||
debug_check!(self) | ||
} | ||
} | ||
|
||
#[godot_api] | ||
#[gen_debug] | ||
impl ScreenEffects { | ||
/// 晃动屏幕 | ||
/// duration: 持续时间 | ||
#[func] | ||
pub fn shake(&mut self, duration: f64, delta: f32) { | ||
// 根据每次等待时间和总时间算出需要震动多少次 | ||
let 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; | ||
} | ||
self.origin_offset = self.base().get_offset(); | ||
self.shake_times = amount; | ||
godot_print!("timer start"); | ||
timer.start(); | ||
} | ||
|
||
#[debug] | ||
fn get_shake_timer(&self) -> Gd<Timer> { | ||
self.base().get_node_as("Shake") | ||
} | ||
|
||
#[func] | ||
fn shake_timeout(&mut self) { | ||
let mut timer = self.get_shake_timer(); | ||
self.shake_times -= 1; | ||
if self.shake_times == 0 { | ||
// 设回原来的位置 | ||
let tmp = self.origin_offset; | ||
self.base_mut().set_offset(tmp); | ||
timer.stop(); | ||
self.shake_delta = 0.0; | ||
return; | ||
} | ||
let rng = -self.shake_delta..=self.shake_delta; | ||
let tmp = self.origin_offset | ||
+ Vector2::new( | ||
rand::thread_rng().gen_range(rng.clone()), | ||
rand::thread_rng().gen_range(rng), | ||
); | ||
self.base_mut().set_offset(tmp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
extends GlobalClass | ||
|
||
@onready var screen_effects_scene = preload("res://scenes/utils/screen_effects.tscn") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[gd_scene format=3 uid="uid://b601ywspu5w2b"] | ||
|
||
[node name="ScreenEffects" type="ScreenEffects"] | ||
position = Vector2(576, 324) | ||
|
||
[node name="Shake" type="Timer" parent="."] | ||
wait_time = 0.1 | ||
|
||
[connection signal="timeout" from="Shake" to="." method="shake_timeout"] |