Skip to content

Commit

Permalink
refactor:fight scene
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jul 6, 2024
1 parent 0a5fce2 commit f117d8d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 38 deletions.
41 changes: 7 additions & 34 deletions gdrust/src/fight.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::OnceLock;

use crate::debug_check;
use crate::fight_items::sword::{SwordManager, START};
use godot::classes::{Control, IControl, Timer};
use godot::obj::WithBaseField;
use godot::prelude::*;
Expand All @@ -13,15 +14,6 @@ struct Fight {
shake_times: i32,
origin_offset: Vector2,
shake_delta: f32,
sword_idx: usize,
}

const START: &str = "start";
const ATTACK_FINISHED: &str = "attack_finished";

fn get_fight_list() -> &'static Vec<&'static str> {
static TMP: OnceLock<Vec<&'static str>> = OnceLock::new();
TMP.get_or_init(|| vec!["EnchantedSword", "StarWrath"])
}

#[godot_api]
Expand All @@ -32,7 +24,6 @@ impl IControl for Fight {
shake_times: 0,
origin_offset: Vector2::ZERO,
shake_delta: 0.0,
sword_idx: 0,
}
}

Expand Down Expand Up @@ -76,23 +67,6 @@ impl Fight {
(camera, timer)
}

fn get_sword(&self, name: &str) -> Gd<Node> {
self.base().get_node_as(name)
}

#[debug]
fn check_sword(&self) {
for i in get_fight_list() {
let obj = self.base().get_node_as::<Node>(*i);
assert!(obj.has_method(START.into()), "**{}** misses start", *i);
assert!(
obj.has_signal(ATTACK_FINISHED.into()),
"**{}** misses attack finished",
*i
);
}
}

#[func]
fn shake_timeout(&mut self) {
let (mut camera, mut timer) = self.get_shake_timer();
Expand All @@ -118,16 +92,15 @@ impl Fight {
self.base().callable("end_fight")
}

#[debug]
fn get_sword_manager(&self) -> Gd<SwordManager> {
self.base().get_node_as("SwordManager")
}

#[func]
fn start_fight(&mut self) {
let list = get_fight_list();
if self.sword_idx >= list.len() {
return;
}
let mut sword = self.get_sword(list[self.sword_idx]);
let mut sword = self.get_sword_manager().bind_mut().get_and_next_sword();
sword.connect("attack_finished".into(), self.get_end_fight());
sword.call(START.into(), &[]);
godot_print!("start fight:{}", self.sword_idx);
self.sword_idx += 1;
}
}
1 change: 1 addition & 0 deletions gdrust/src/fight_items.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bar;
mod block_drawer;
mod health_bar;
pub mod sword;
72 changes: 72 additions & 0 deletions gdrust/src/fight_items/sword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use derive::gen_debug;
use godot::{
engine::{INode, Node},
obj::{Base, Gd, WithBaseField},
register::{godot_api, GodotClass},
};
use std::sync::OnceLock;

use crate::debug_check;

#[derive(GodotClass)]
#[class(base = Node)]
pub struct SwordManager {
sword_idx: usize,
base: Base<Node>,
}

pub const START: &str = "start";
pub const ATTACK_FINISHED: &str = "attack_finished";

fn get_fight_list() -> &'static Vec<&'static str> {
static TMP: OnceLock<Vec<&'static str>> = OnceLock::new();
TMP.get_or_init(|| vec!["EnchantedSword", "StarWrath"])
}

#[godot_api]
impl INode for SwordManager {
fn init(base: Base<Node>) -> Self {
Self { base, sword_idx: 0 }
}

fn ready(&mut self) {
debug_check!(self);
}
}

#[godot_api]
#[gen_debug]
impl SwordManager {
#[debug]
fn check_sword(&self) {
for i in get_fight_list() {
let obj = self.base().get_node_as::<Node>(*i);
assert!(obj.has_method(START.into()), "**{}** misses start", *i);
assert!(
obj.has_signal(ATTACK_FINISHED.into()),
"**{}** misses attack finished",
*i
);
}
}

#[func]
fn get_sword(&mut self) -> Gd<Node> {
self.base().get_node_as(get_fight_list()[self.sword_idx])
}

#[func]
fn next_sword(&mut self) {
if self.sword_idx >= get_fight_list().len() {
return;
}
self.sword_idx += 1;
}

#[func]
pub fn get_and_next_sword(&mut self) -> Gd<Node> {
let obj = self.get_sword();
self.next_sword();
obj
}
}
8 changes: 4 additions & 4 deletions scenes/fight.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ scale = Vector2(3.28571, 3.28571)
[node name="image" type="Sprite2D" parent="ZenithBegin"]
texture = ExtResource("3_2spkb")

[node name="Swords" type="Node" parent="."]
[node name="SwordManager" type="SwordManager" parent="."]

[node name="StarWrath" parent="Swords" instance=ExtResource("4_8ipx5")]
[node name="StarWrath" parent="SwordManager" instance=ExtResource("4_8ipx5")]

[node name="EnchantedSword" parent="Swords" instance=ExtResource("13_1n0f0")]
[node name="EnchantedSword" parent="SwordManager" instance=ExtResource("13_1n0f0")]
rotation = 0.710345

[node name="Seedler" parent="Swords" instance=ExtResource("4_hakuj")]
[node name="Seedler" parent="SwordManager" instance=ExtResource("4_hakuj")]

[node name="Player" type="Player" parent="."]
position = Vector2(570, 414)
Expand Down
3 changes: 3 additions & 0 deletions scripts/weapons/seedler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func move(length: float) -> void:


func _ready() -> void:
if get_tree().current_scene != $"..":
hide()
return
rotation = 0
follow.loop = 1
var start = 0
Expand Down

0 comments on commit f117d8d

Please sign in to comment.