diff --git a/DungeonShooting_Godot/prefab/test/TestActivity.tscn b/DungeonShooting_Godot/prefab/test/TestActivity.tscn new file mode 100644 index 0000000..7f76154 --- /dev/null +++ b/DungeonShooting_Godot/prefab/test/TestActivity.tscn @@ -0,0 +1,42 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://resource/materlal/Blend.gdshader" type="Shader" id=1] +[ext_resource path="res://addons/dungeonShooting_plugin/ActivityObjectTemplate.cs" type="Script" id=2] +[ext_resource path="res://icon.png" type="Texture" id=3] + +[sub_resource type="ShaderMaterial" id=1] +resource_local_to_scene = true +render_priority = 4 +shader = ExtResource( 1 ) +shader_param/blend = Color( 0, 0, 0, 0.470588 ) +shader_param/schedule = 1 + +[sub_resource type="ShaderMaterial" id=2] +resource_local_to_scene = true +render_priority = 4 +shader = ExtResource( 1 ) +shader_param/blend = Color( 1, 1, 1, 1 ) +shader_param/schedule = 0 + +[sub_resource type="SpriteFrames" id=3] +animations = [ { +"frames": [ ExtResource( 3 ) ], +"loop": true, +"name": "default", +"speed": 5.0 +} ] + +[node name="TestActivity" type="Node"] +script = ExtResource( 2 ) + +[node name="ShadowSprite" type="Sprite" parent="."] +material = SubResource( 1 ) +z_index = -5 + +[node name="AnimatedSprite" type="AnimatedSprite" parent="."] +material = SubResource( 2 ) +frames = SubResource( 3 ) + +[node name="Collision" type="CollisionShape2D" parent="."] + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] diff --git a/DungeonShooting_Godot/src/framework/ActivityObject.cs b/DungeonShooting_Godot/src/framework/ActivityObject.cs index 17f66f7..ef1a1cd 100644 --- a/DungeonShooting_Godot/src/framework/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/ActivityObject.cs @@ -59,6 +59,11 @@ /// 阴影偏移 /// public Vector2 ShadowOffset { get; protected set; } = new Vector2(0, 2); + + /// + /// 移动控制器 + /// + public MoveController MoveController { get; } //组件集合 private List> _components = new List>(); @@ -125,6 +130,9 @@ break; } } + + MoveController = new MoveController(); + AddComponent(MoveController); } /// @@ -427,7 +435,7 @@ if (component == null) return null; return (TC)component; } - + public override void _Process(float delta) { //更新组件 diff --git a/DungeonShooting_Godot/src/framework/MoveController.cs b/DungeonShooting_Godot/src/framework/MoveController.cs index e7f28aa..5f79c96 100644 --- a/DungeonShooting_Godot/src/framework/MoveController.cs +++ b/DungeonShooting_Godot/src/framework/MoveController.cs @@ -2,32 +2,23 @@ using System.Collections.Generic; using Godot; +/// +/// 移动控制器, 物体运动由力来控制, 不同方向的力速度最终会汇总 +/// public class MoveController : Component { /// /// 玩家受到的外力的集合 /// - private readonly Dictionary _forceData = new Dictionary(); + private readonly List _forceList = new List(); /// /// 这个速度就是玩家当前物理帧移动的真实速率 - /// 该速度就是 BasisVelocity + ForceVelocity /// public Vector2 Velocity => _velocity; private Vector2 _velocity = Vector2.Zero; /// - /// 玩家的基础移动速率, 包括左右移动, 下坠, 上升等 - /// - public Vector2 BasisVelocity { get => _basisVelocity; set => _basisVelocity = value; } - private Vector2 _basisVelocity = Vector2.Zero; - - /// - /// 玩家向上的方向 - /// - private readonly Vector2 _upDir = Vector2.Up; - - /// /// 根据名称添加一个外力, 并返回创建的外力的对象, 如果存在这个名称的外力, 移除之前的外力 /// public ExternalForce AddForce(string name) @@ -42,8 +33,8 @@ /// public T AddForce(T force) where T : ExternalForce { - _forceData.Remove(force.Name); - _forceData.Add(force.Name, force); + RemoveForce(force.Name); + _forceList.Add(force); return force; } @@ -52,9 +43,13 @@ /// public void RemoveForce(string name) { - if (!_forceData.Remove(name)) + for (var i = 0; i < _forceList.Count; i++) { - GD.PrintErr($"力:{name}不存在!!!"); + if (_forceList[i].Name == name) + { + _forceList.RemoveAt(i); + return; + } } } @@ -63,8 +58,16 @@ /// public ExternalForce GetForce(string name) { - _forceData.TryGetValue(name, out var f); - return f; + for (var i = 0; i < _forceList.Count; i++) + { + var externalForce = _forceList[i]; + if (externalForce.Name == name) + { + return externalForce; + } + } + + return null; } /// @@ -72,7 +75,16 @@ /// public bool ContainsForce(string name) { - return _forceData.ContainsKey(name); + for (var i = 0; i < _forceList.Count; i++) + { + var externalForce = _forceList[i]; + if (externalForce.Name == name) + { + return true; + } + } + + return false; } /// @@ -88,7 +100,7 @@ /// public void ClearForce() { - _forceData.Clear(); + _forceList.Clear(); } /// @@ -97,49 +109,49 @@ public void Halt() { ClearForce(); - _basisVelocity = Vector2.Zero; + _velocity = Vector2.Zero; } public override void PhysicsProcess(float delta) { //先调用更新 - var ks = new List(_forceData.Keys); - foreach (var k in ks) + var externalForces = _forceList.ToArray(); + foreach (var fore in externalForces) { - var fore = _forceData[k]; if (fore.Enable) fore.PhysicsProcess(delta); } //外力总和 var finallyEf = Vector2.Zero; - foreach (var item in _forceData) + foreach (var fore in externalForces) { - if (item.Value.Enable) - finallyEf += item.Value.Velocity; + if (fore.Enable) + finallyEf += fore.Velocity; } + //最终速率 - var finallyVelocity = _basisVelocity + finallyEf; + var finallyVelocity = _velocity + finallyEf; //计算移动 - _velocity = ActivityObject.MoveAndSlide(finallyVelocity, _upDir); + _velocity = ActivityObject.MoveAndSlide(finallyVelocity); //调整外力速率 - if (_forceData.Count > 0) + if (externalForces.Length > 0) { var scale = new Vector2(); //x轴外力 - var efx = _velocity.x - _basisVelocity.x; + var efx = _velocity.x - _velocity.x; scale.x = finallyEf.x == 0f ? 0 : Mathf.Abs(efx / finallyEf.x); //y轴外力 - var efy = _velocity.y - _basisVelocity.y; + var efy = _velocity.y - _velocity.y; scale.y = finallyEf.y == 0f ? 0 : Mathf.Abs(efy / finallyEf.y); - foreach (var item in _forceData) + foreach (var force in externalForces) { - if (item.Value.Enable) + if (force.Enable) { - var velocity = item.Value.Velocity; - item.Value.Velocity = new Vector2(velocity.x * scale.x, velocity.y * scale.y); + var velocity = force.Velocity; + force.Velocity = new Vector2(velocity.x * scale.x, velocity.y * scale.y); } } } diff --git a/DungeonShooting_Godot/src/game/manager/ResourcePath.cs b/DungeonShooting_Godot/src/game/manager/ResourcePath.cs index 48840a6..8a95f1c 100644 --- a/DungeonShooting_Godot/src/game/manager/ResourcePath.cs +++ b/DungeonShooting_Godot/src/game/manager/ResourcePath.cs @@ -20,6 +20,7 @@ public const string prefab_role_Player_tscn = "res://prefab/role/Player.tscn"; public const string prefab_role_Role_tscn = "res://prefab/role/Role.tscn"; public const string prefab_test_MoveComponent_tscn = "res://prefab/test/MoveComponent.tscn"; + public const string prefab_test_TestActivity_tscn = "res://prefab/test/TestActivity.tscn"; public const string prefab_ui_Cursor_tscn = "res://prefab/ui/Cursor.tscn"; public const string prefab_ui_RoomUI_tscn = "res://prefab/ui/RoomUI.tscn"; public const string prefab_ui_bar_InteractiveTipBar_tscn = "res://prefab/ui/bar/InteractiveTipBar.tscn"; @@ -38,6 +39,7 @@ public const string resource_font_cn_font_12_tres = "res://resource/font/cn_font_12.tres"; public const string resource_font_cn_font_18_tres = "res://resource/font/cn_font_18.tres"; public const string resource_font_cn_font_36_tres = "res://resource/font/cn_font_36.tres"; + public const string resource_map_dungeon_1_tmx = "res://resource/map/dungeon_1.tmx"; public const string resource_map_dungeon_test_tmx = "res://resource/map/dungeon_test.tmx"; public const string resource_map_itchioDungeonTileset4_tsx = "res://resource/map/itch-io-DungeonTileset4.tsx"; public const string resource_materlal_Blend_gdshader = "res://resource/materlal/Blend.gdshader"; diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index dfa647d..f96497a 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -66,7 +66,10 @@ Player.Position = new Vector2(100, 100); Player.Name = "Player"; Player.PutDown(); - + + // var testActivity = new TestActivity(); + // testActivity.Position = new Vector2(10, 10); + // testActivity.PutDown(); } public override void _Ready() diff --git a/DungeonShooting_Godot/src/test/TestActivity.cs b/DungeonShooting_Godot/src/test/TestActivity.cs new file mode 100644 index 0000000..f2dd5a2 --- /dev/null +++ b/DungeonShooting_Godot/src/test/TestActivity.cs @@ -0,0 +1,10 @@ +using Godot; + +public class TestActivity : ActivityObject +{ + public TestActivity() : base(ResourcePath.prefab_test_TestActivity_tscn) + { + var externalForce = MoveController.AddForce("move"); + externalForce.Velocity = new Vector2(0, 60); + } +} \ No newline at end of file