From 7ac065819687e81dc7f2f0c801faa2efd74aa4df Mon Sep 17 00:00:00 2001 From: limuy2022 Date: Sat, 8 Jun 2024 16:50:31 +0800 Subject: [PATCH] prepare for cthulhu --- gdrust/src/lib.rs | 21 ++++- gdrust/src/player.rs | 85 ++++++++++-------- .../images/bullets/Seedler/Seedler_Nut.png | Bin 0 -> 233 bytes .../bullets/Seedler/Seedler_Nut.png.import | 34 +++++++ .../images/bullets/Seedler/Seedler_Thorns.png | Bin 0 -> 173 bytes .../bullets/Seedler/Seedler_Thorns.png.import | 34 +++++++ resources/images/weapons/Seedler.png | Bin 0 -> 939 bytes resources/images/weapons/Seedler.png.import | 34 +++++++ scenes/bullets/seedler_nut.tscn | 17 ++++ scenes/bullets/seedler_thorns.tscn | 18 ++++ scenes/weapons/seedler.tscn | 8 ++ scripts/weapons/enchanted_sword.gd | 8 ++ 12 files changed, 223 insertions(+), 36 deletions(-) create mode 100644 resources/images/bullets/Seedler/Seedler_Nut.png create mode 100644 resources/images/bullets/Seedler/Seedler_Nut.png.import create mode 100644 resources/images/bullets/Seedler/Seedler_Thorns.png create mode 100644 resources/images/bullets/Seedler/Seedler_Thorns.png.import create mode 100644 resources/images/weapons/Seedler.png create mode 100644 resources/images/weapons/Seedler.png.import create mode 100644 scenes/bullets/seedler_nut.tscn create mode 100644 scenes/bullets/seedler_thorns.tscn create mode 100644 scenes/weapons/seedler.tscn diff --git a/gdrust/src/lib.rs b/gdrust/src/lib.rs index 97c8108..8f75de0 100644 --- a/gdrust/src/lib.rs +++ b/gdrust/src/lib.rs @@ -7,8 +7,27 @@ mod weapons; mod zenith; use godot::prelude::*; +use std::panic::{set_hook, PanicInfo}; struct GdExtension; +fn panic_handler(info: &PanicInfo) { + if let Some(p) = info.location() { + godot_error!( + "Panic occurred in file '{}' at line {}\n", + p.file(), + p.line() + ); + } else { + godot_error!("Panic occurred but can't get location information."); + } +} + #[gdextension()] -unsafe impl ExtensionLibrary for GdExtension {} +unsafe impl ExtensionLibrary for GdExtension { + fn on_level_init(level: InitLevel) { + if level == InitLevel::Scene { + set_hook(Box::new(panic_handler)) + } + } +} diff --git a/gdrust/src/player.rs b/gdrust/src/player.rs index 0e1ab12..cb51553 100644 --- a/gdrust/src/player.rs +++ b/gdrust/src/player.rs @@ -12,7 +12,7 @@ pub struct Player { base: Base, status: Movement, /// 上一次点击克盾冲刺键的时间,记录来判断双击 - click_time: Instant, + click_time: Option, /// 上一次的类型 click_type: Click, } @@ -41,6 +41,7 @@ const MOVE_LEFT: &str = "move_left"; const MOVE_UP: &str = "move_up"; const MOVE_DOWN: &str = "move_down"; const SLOW_DOWN: &str = "slow_down"; +const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500); #[godot_api()] impl ICharacterBody2D for Player { @@ -50,7 +51,7 @@ impl ICharacterBody2D for Player { base, health: Self::MAX_HEALTH, status: Movement::Move, - click_time: Instant::now(), + click_time: None, click_type: Click::None, } } @@ -59,16 +60,16 @@ impl ICharacterBody2D for Player { fn physics_process(&mut self, delta: f64) { let input_obj = Input::singleton(); - let mut speed = Self::SPEED; + let mut down_rate = 1.0; if input_obj.is_action_pressed(SLOW_DOWN.into()) { - speed /= 2; + down_rate = 0.5; } if self.status == Movement::Rush { godot_print!("rush inside!"); let vel = Vector2::new( match self.click_type { - Click::Left => -Self::RUSH_SPEED as f32, - Click::Right => Self::RUSH_SPEED as f32, + Click::Left => -1.0, + Click::Right => 1.0, _ => { let msg = format!("wrong movement {:?} when rush", self.click_type); godot_error!("{}", &msg); @@ -77,8 +78,9 @@ impl ICharacterBody2D for Player { }, 0.0, ); - self.base_mut() - .move_and_collide(vel * delta as f32 * speed as f32); + self.base_mut().move_and_collide( + vel.normalized() * Self::RUSH_SPEED as f32 * delta as f32 * down_rate as f32, + ); return; } let mut vel = Vector2::ZERO; @@ -89,32 +91,15 @@ impl ICharacterBody2D for Player { vel.x += 1.0; } // 触发克盾 - if input_obj.is_action_just_pressed(MOVE_LEFT.into()) - && self.click_type == Click::Left - && self.click_time.elapsed() < Duration::new(0, 100 * 1000) - { - self.start_rush(); - } - if input_obj.is_action_just_pressed(MOVE_LEFT.into()) - && self.click_type == Click::Left - && self.click_time.elapsed() < Duration::new(0, 100 * 1000) - { - self.start_rush(); - } - // 记录克盾第一次按按钮 - if input_obj.is_action_just_released(MOVE_RIGHT.into()) { - self.click_time = Instant::now(); - self.click_type = Click::Right; - } - if input_obj.is_action_just_released(MOVE_LEFT.into()) { - self.click_time = Instant::now(); - self.click_type = Click::Left; + self.process_rush(MOVE_LEFT.into(), Click::Left); + self.process_rush(MOVE_RIGHT.into(), Click::Right); + if input_obj.is_action_just_pressed(MOVE_UP.into()) { + self.click_type = Click::Up; + self.click_time = None; } - if input_obj.is_action_just_released(MOVE_DOWN.into()) { + if input_obj.is_action_just_pressed(MOVE_DOWN.into()) { self.click_type = Click::Down; - } - if input_obj.is_action_just_released(MOVE_UP.into()) { - self.click_type = Click::Up; + self.click_time = None; } if input_obj.is_action_pressed(MOVE_UP.into()) { vel.y -= 1.0; @@ -124,7 +109,7 @@ impl ICharacterBody2D for Player { } let res = self .base_mut() - .move_and_collide(vel.normalized() * speed as f32 * delta as f32); + .move_and_collide(vel.normalized() * Self::SPEED as f32 * down_rate * delta as f32); } } @@ -147,7 +132,6 @@ impl Player { } fn start_rush(&mut self) { - godot_print!("rush inside!"); self.status = Movement::Rush; // 冲刺结束计时器 let mut timer = self.base().get_node_as::("Cthulhu"); @@ -163,10 +147,41 @@ impl Player { #[func] fn stop_rush(&mut self) { - godot_print!("rush!"); let mut particle = self.get_virtual_particle(); particle.set_emitting(false); self.status = Movement::Move; self.click_type = Click::None; } + + fn process_rush(&mut self, movement_name: StringName, click_type: Click) { + let input_obj = Input::singleton(); + if input_obj.is_action_just_pressed(movement_name) { + godot_print!("enter rust process"); + // 先检查上一次按下的键是不是双击键 + if self.click_type != click_type { + self.click_type = click_type; + return; + } // 判断第一次还是第二次 + match self.click_time { + None => { + // 第一次按下按钮,记录时间 + self.click_time = Some(Instant::now()) + } + Some(t) => { + // 第二次按下,比较两次中间的间隔 + let dur = t.elapsed(); + if dur <= DOUBLE_CLICK_DURATION { + // 达成双击条件 + // 清除双击时间 + self.click_time = None; + self.click_type = Click::None; + self.start_rush(); + } else { + // 否则将当前时间设为双击开头 + self.click_time = Some(Instant::now()); + } + } + } + } + } } diff --git a/resources/images/bullets/Seedler/Seedler_Nut.png b/resources/images/bullets/Seedler/Seedler_Nut.png new file mode 100644 index 0000000000000000000000000000000000000000..140d08ad4e8e8f946bba542a2f58ebd25b3e4d46 GIT binary patch literal 233 zcmeAS@N?(olHy`uVBq!ia0vp^0zk~i!3HGN^yhQ|DaPU;cPEB*=VV?2Ig34A978Pp zlT#d+`PiiXUpzYDC%fLA1ea%v3mk+}q!}*nurcM%Ie7H+^gx+S@2@!<{J*l>JwfW3 z&<^esW)8u@!f&3}TF%rki?9DJdX#n6p*sn7XH;j;w`cn-scF^_u;5|0;0A6DGlplZ z@t%}8SFp&^^1R<-~D literal 0 HcmV?d00001 diff --git a/resources/images/bullets/Seedler/Seedler_Nut.png.import b/resources/images/bullets/Seedler/Seedler_Nut.png.import new file mode 100644 index 0000000..978ff00 --- /dev/null +++ b/resources/images/bullets/Seedler/Seedler_Nut.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://g6vbpvi4ja4e" +path="res://.godot/imported/Seedler_Nut.png-463cb8f60b039abee762a8c3697237e1.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://resources/images/bullets/Seedler/Seedler_Nut.png" +dest_files=["res://.godot/imported/Seedler_Nut.png-463cb8f60b039abee762a8c3697237e1.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/resources/images/bullets/Seedler/Seedler_Thorns.png b/resources/images/bullets/Seedler/Seedler_Thorns.png new file mode 100644 index 0000000000000000000000000000000000000000..f2170ca790819f76d6dbcd09fc8309d48ae94f7b GIT binary patch literal 173 zcmeAS@N?(olHy`uVBq!ia0vp^AT}Qd8;}%R+`Ae`F%}28J29*~C-V}>N%wSd4ABTq zPH|x7W0U&t=)|sfC&6W!)58W8HV4Hsr=G5iOjz!EZXVmR#Jtws6H{76#kM6(w(^rX zmapX3YM`CS6?Vsx*G$vtsL0V2w?(3doaC1s^zq=+DS35Q`A?q83W2*Rn;uIs2&)C! UK3@OjCeTU-Pgg&ebxsLQ0EWRh>Hq)$ literal 0 HcmV?d00001 diff --git a/resources/images/bullets/Seedler/Seedler_Thorns.png.import b/resources/images/bullets/Seedler/Seedler_Thorns.png.import new file mode 100644 index 0000000..8abc018 --- /dev/null +++ b/resources/images/bullets/Seedler/Seedler_Thorns.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://betqbnnoc7vvq" +path="res://.godot/imported/Seedler_Thorns.png-7e1952da7bde014cff20016b1ff91c97.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://resources/images/bullets/Seedler/Seedler_Thorns.png" +dest_files=["res://.godot/imported/Seedler_Thorns.png-7e1952da7bde014cff20016b1ff91c97.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/resources/images/weapons/Seedler.png b/resources/images/weapons/Seedler.png new file mode 100644 index 0000000000000000000000000000000000000000..8b3a4e3f8e1956e7c33dcb8de5ec5a11eadf8ac3 GIT binary patch literal 939 zcmV;c162HpP)Px&Wl2OqRA@u(nq5d#K@`W^ia;a?vRW;S9-_8ywkO$#lC+E@Um}7&&=90Z@F9x8 zmq-wzhk_6yDIp|?BCx2?vImi(XqSrACmS!aF zJ7KYcld(BOw!ZQs#n)Y1DBkb6p+OA{VX=Zw9UUXG*Y+=R^m5<`2UalB*^A{Lh~&yu zD!zNWf+B?Xo~+Qk>>FXRg1>L?CxWpul_k0!murN%mwhKJMldfJrIC3=_U5BWM=yi4 zZ-m7PKG^+P>#;r!|I)mC`}8jQ-dxo~(Rae)1p{|`N0|N$JmLR>wHZ>t7i0Iyk}dHW-MOmkR{schy2OIAS_0(sU+LHi~S1EE1ej@w#jDmE>_Q; z5f&?$Pqu8mi&Ww{VKIWUZPMlfFtqHCJG zqIMBmA`2)ki(2=gZc&+_}&4q0rT<=3tKuvo$88rRTZ z`>sz=yfFWbBb;N6%Io;p&W>k{uo%JIs}Nq7cuJR^bqr9vdS?^Gi=A^Rt{z)L(bgNd zPFTEPWQ*3AI`!EXPEf4ddW2&2fy<7Bvu&De#I0F^6BZ{Jl@D2>q;@@x>1#s0von=a z=WIXkrL>C3H?H&UC2Ws?6Arv!cyA=Ey0p8sl@yVW(PN;S!|&|_y_(%>8mj5to*$c) z=g|%3nnkQ079*IKHwOXFdcQ*5qj$R_)9wLpfIKkYDg_7m>