From df2bc47ee65bba335439d8825862d79035d0c9f9 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Thu, 5 Sep 2024 19:41:55 +0800 Subject: [PATCH] add a example of Storage --- Example/Action3D/MainGame.tscn | 2 + Example/Action3D/Script/ActionCamera.cs | 4 +- Example/Action3D/Script/ActionPlayer.cs | 19 ++++--- Example/SystemExamples/ItemList/HPPotion.tres | 9 ++++ .../PlayerStorage/PlayerStorage.tscn | 12 +++++ .../PlayerStorage/Script/DataStorageItem.cs | 36 +++++++++++++ .../PlayerStorage/Script/InterfaceStorage.cs | 23 ++++++++ .../PlayerStorage/Script/ViewStorage.cs | 19 +++++++ .../Module/NodeExtension/EGCreate.cs | 2 +- .../Module/SaveTools/EGByteSave.cs | 47 ++++++++++++++++ addons/Tools/ItemImporter/ToolItemImporter.cs | 54 +++++++++++++++++++ .../Tools/ItemImporter/ToolItemImporter.tscn | 37 +++++++++++++ addons/Tools/ItemImporter/plugin.cfg | 7 +++ project.godot | 4 ++ 14 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 Example/SystemExamples/ItemList/HPPotion.tres create mode 100644 Example/SystemExamples/PlayerStorage/PlayerStorage.tscn create mode 100644 Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs create mode 100644 Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs create mode 100644 Example/SystemExamples/PlayerStorage/Script/ViewStorage.cs create mode 100644 addons/EGFramework/Module/SaveTools/EGByteSave.cs create mode 100644 addons/Tools/ItemImporter/ToolItemImporter.cs create mode 100644 addons/Tools/ItemImporter/ToolItemImporter.tscn create mode 100644 addons/Tools/ItemImporter/plugin.cfg diff --git a/Example/Action3D/MainGame.tscn b/Example/Action3D/MainGame.tscn index 0c8e5cb..34ba4cd 100644 --- a/Example/Action3D/MainGame.tscn +++ b/Example/Action3D/MainGame.tscn @@ -92,6 +92,8 @@ transform = Transform3D(1, 0, 0, 0, 0.976296, 0.21644, 0, -0.21644, 0.976296, 0, [node name="Player" type="CharacterBody3D" parent="."] script = ExtResource("2_c0s4j") +JumpSpeed = 2.0 +FallAcceleration = 9.8 [node name="CollisionShape3D" type="CollisionShape3D" parent="Player"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0296049, -9.53674e-07, 0.00402832) diff --git a/Example/Action3D/Script/ActionCamera.cs b/Example/Action3D/Script/ActionCamera.cs index 69f5f29..5f6dadf 100644 --- a/Example/Action3D/Script/ActionCamera.cs +++ b/Example/Action3D/Script/ActionCamera.cs @@ -28,11 +28,11 @@ public partial class ActionCamera : Node3D } if (Input.IsActionPressed("camera_left")) { - _targetRotation.Y -= Speed*(float)delta; + _targetRotation.Y += Speed*(float)delta; } if (Input.IsActionPressed("camera_right")) { - _targetRotation.Y += Speed*(float)delta; + _targetRotation.Y -= Speed*(float)delta; } if (_targetRotation.X>30*Mathf.Pi/180) { diff --git a/Example/Action3D/Script/ActionPlayer.cs b/Example/Action3D/Script/ActionPlayer.cs index c42a2b4..e2b8315 100644 --- a/Example/Action3D/Script/ActionPlayer.cs +++ b/Example/Action3D/Script/ActionPlayer.cs @@ -5,9 +5,13 @@ namespace EGFramework.Examples.Action3D{ public partial class ActionPlayer : CharacterBody3D { [Export] - public int Speed { get; set; } = 14; + public float Speed { get; set; } = 14; [Export] - public int FallAcceleration { get; set; } = 75; + public float JumpSpeed { set; get; } = 15; + [Export] + public float FallAcceleration { get; set; } = 75; + + public float FallSpeed { get; set; } = 0; private Vector3 _targetVelocity = Vector3.Zero; @@ -34,7 +38,8 @@ namespace EGFramework.Examples.Action3D{ if (direction.Length() > 0) { - this.Rotation = new Vector3(0,CameraPivot.Rotation.Y,0); + // this.Rotation = new Vector3(0,CameraPivot.Rotation.Y,0); + this.GetNode("Body").Rotation = new Vector3(0,CameraPivot.Rotation.Y,0); direction = direction.Normalized(); direction = direction.Rotated(Vector3.Up, CameraPivot.Rotation.Y); } @@ -45,12 +50,14 @@ namespace EGFramework.Examples.Action3D{ if (!IsOnFloor()) // If in the air, fall towards the floor. Literally gravity { - // GD.Print("Fallen!"); - _targetVelocity.Y -= FallAcceleration * (float)delta; + // GD.Print(FallSpeed); + FallSpeed -= FallAcceleration * (float)delta; + _targetVelocity.Y += FallSpeed; }else{ if (Input.IsActionPressed("jump")) { - _targetVelocity.Y += 10.0f; + _targetVelocity.Y = 0.1f; + FallSpeed = JumpSpeed; } } diff --git a/Example/SystemExamples/ItemList/HPPotion.tres b/Example/SystemExamples/ItemList/HPPotion.tres new file mode 100644 index 0000000..d57c54a --- /dev/null +++ b/Example/SystemExamples/ItemList/HPPotion.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" load_steps=2 format=3 uid="uid://bhsavl0d80s6j"] + +[ext_resource type="Script" path="res://Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs" id="1_j773b"] + +[resource] +script = ExtResource("1_j773b") +Id = 0 +Name = "" +Info = "" diff --git a/Example/SystemExamples/PlayerStorage/PlayerStorage.tscn b/Example/SystemExamples/PlayerStorage/PlayerStorage.tscn new file mode 100644 index 0000000..fb8d2b5 --- /dev/null +++ b/Example/SystemExamples/PlayerStorage/PlayerStorage.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=2 format=3 uid="uid://tee7nc525si7"] + +[ext_resource type="Script" path="res://Example/SystemExamples/PlayerStorage/Script/ViewStorage.cs" id="1_goow2"] + +[node name="PlayerStorage" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_goow2") diff --git a/Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs b/Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs new file mode 100644 index 0000000..2ea02a3 --- /dev/null +++ b/Example/SystemExamples/PlayerStorage/Script/DataStorageItem.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Godot; + +namespace EGFramework.Example.SystemExamples.PlayerStorage +{ + public partial class DataStorageItem : Resource, IItem + { + [Export] public int Id { set; get; } + [Export] public string Name { set; get; } + [Export(PropertyHint.MultilineText)] public string Info { set; get; } + [Export] public Texture Icon { set; get; } + + public Texture GetIcon() + { + throw new NotImplementedException(); + } + + public string GetInfo() + { + throw new NotImplementedException(); + } + + public int GetItemId() + { + throw new NotImplementedException(); + } + + public string GetName() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs b/Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs new file mode 100644 index 0000000..0fb926e --- /dev/null +++ b/Example/SystemExamples/PlayerStorage/Script/InterfaceStorage.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Godot; + +namespace EGFramework.Example.SystemExamples.PlayerStorage +{ + public interface IItem + { + public int GetItemId(); + public string GetName(); + public Texture GetIcon(); + public string GetInfo(); + } + public interface IBackPackItem : IItem{ + public int GetCount(); + } + + public interface ICostItem: IItem { + public void OnCost(); + } +} \ No newline at end of file diff --git a/Example/SystemExamples/PlayerStorage/Script/ViewStorage.cs b/Example/SystemExamples/PlayerStorage/Script/ViewStorage.cs new file mode 100644 index 0000000..34e750e --- /dev/null +++ b/Example/SystemExamples/PlayerStorage/Script/ViewStorage.cs @@ -0,0 +1,19 @@ +using Godot; +using System; +using EGFramework; + +public partial class ViewStorage : Node,IEGFramework +{ + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + // this.GetModule().SaveToFile("nihao"); + Variant result = this.GetModule().LoadFromFile(); + GD.Print(result); + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + } +} diff --git a/addons/EGFramework/Module/NodeExtension/EGCreate.cs b/addons/EGFramework/Module/NodeExtension/EGCreate.cs index 2c35109..31ac00c 100644 --- a/addons/EGFramework/Module/NodeExtension/EGCreate.cs +++ b/addons/EGFramework/Module/NodeExtension/EGCreate.cs @@ -42,7 +42,7 @@ namespace EGFramework{ } public static void Alert(this Node self,string alertMsg){ - + } } } diff --git a/addons/EGFramework/Module/SaveTools/EGByteSave.cs b/addons/EGFramework/Module/SaveTools/EGByteSave.cs new file mode 100644 index 0000000..ffb266e --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGByteSave.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Runtime.Serialization.Formatters.Binary; +using Godot; + +namespace EGFramework +{ + public class EGByteSave : EGModule//, IEGSave + { + + public void SaveToFile(string content) + { + using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Write); + Variant hp = 10; + file.StoreVar(hp); + Variant pos = new Vector2(100,100); + file.StoreVar(pos); + } + + public Variant LoadFromFile() + { + using var file = FileAccess.Open("user://save_game.dat", FileAccess.ModeFlags.Read); + Variant content = file.GetVar(); + Variant pos = file.GetVar(); + return pos; + } + public override void Init() + { + + } + + // public TData GetDataByFile() where TData : class, new() + // { + // throw new NotImplementedException(); + // } + // public void InitSaveData(string fileName) + // { + // throw new NotImplementedException(); + // } + public void SetDataToFile(TData data) + { + + } + } +} \ No newline at end of file diff --git a/addons/Tools/ItemImporter/ToolItemImporter.cs b/addons/Tools/ItemImporter/ToolItemImporter.cs new file mode 100644 index 0000000..a9c3913 --- /dev/null +++ b/addons/Tools/ItemImporter/ToolItemImporter.cs @@ -0,0 +1,54 @@ +#if TOOLS +using Godot; +using System; + +namespace EGFramework.Example.SystemExamples.PlayerStorage +{ + [Tool] + public partial class ToolItemImporter : EditorPlugin + { + PackedScene MainPanel = ResourceLoader.Load("res://addons/Tools/ItemImporter/ToolItemImporter.tscn"); + Control MainPanelInstance; + public override void _EnterTree() + { + // Initialization of the plugin goes here. + MainPanelInstance = (Control)MainPanel.Instantiate(); + // Add the main panel to the editor's main viewport. + EditorInterface.Singleton.GetEditorMainScreen().AddChild(MainPanelInstance); + // Hide the main panel. Very much required. + _MakeVisible(false); + } + + public override void _ExitTree() + { + // Clean-up of the plugin goes here. + if (MainPanelInstance != null) + { + MainPanelInstance.QueueFree(); + } + } + public override bool _HasMainScreen() + { + return true; + } + + public override void _MakeVisible(bool visible) + { + if (MainPanelInstance != null) + { + MainPanelInstance.Visible = visible; + } + } + + public override string _GetPluginName() + { + return "Tool Item Importer"; + } + + public override Texture2D _GetPluginIcon() + { + return EditorInterface.Singleton.GetEditorTheme().GetIcon("Node", "EditorIcons"); + } + } +} +#endif diff --git a/addons/Tools/ItemImporter/ToolItemImporter.tscn b/addons/Tools/ItemImporter/ToolItemImporter.tscn new file mode 100644 index 0000000..0cc55a3 --- /dev/null +++ b/addons/Tools/ItemImporter/ToolItemImporter.tscn @@ -0,0 +1,37 @@ +[gd_scene format=3 uid="uid://bglw1yht7w1kt"] + +[node name="ToolItemImporter" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Button" type="Button" parent="."] +layout_mode = 1 +offset_right = 69.0 +offset_bottom = 31.0 +text = "导入CSV" + +[node name="Button2" type="Button" parent="."] +layout_mode = 1 +anchors_preset = 1 +anchor_left = 1.0 +anchor_right = 1.0 +offset_left = -72.0 +offset_bottom = 31.0 +grow_horizontal = 0 +text = "重新载入" + +[node name="Button3" type="Button" parent="."] +layout_mode = 1 +anchors_preset = 1 +anchor_left = 1.0 +anchor_right = 1.0 +offset_left = -71.0 +offset_top = 36.0 +offset_right = 1.0 +offset_bottom = 67.0 +grow_horizontal = 0 +text = "重新载入" diff --git a/addons/Tools/ItemImporter/plugin.cfg b/addons/Tools/ItemImporter/plugin.cfg new file mode 100644 index 0000000..b1c3cf3 --- /dev/null +++ b/addons/Tools/ItemImporter/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="ItemImporter" +description="a csv ItemImporter" +author="jkpete" +version="" +script="ToolItemImporter.cs" \ No newline at end of file diff --git a/project.godot b/project.godot index 77e84e6..6e27992 100644 --- a/project.godot +++ b/project.godot @@ -20,6 +20,10 @@ config/icon="res://icon.svg" project/assembly_name="EGFramework" +[editor_plugins] + +enabled=PackedStringArray("res://addons/Tools/ItemImporter/plugin.cfg") + [input] move_forward={