From 1c116eb820c2a8910749bad3358785f1422b2728 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Tue, 7 May 2024 19:36:06 +0800 Subject: [PATCH] =?UTF-8?q?Add=20a=20null=20judgment.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=A9=BA=E5=88=A4=E6=96=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 5 +- locals/Log.en.translation | Bin 770 -> 862 bytes locals/Log.zh.translation | Bin 861 -> 973 bytes project.godot | 1 - scripts/GameSceneNodeHolder.cs | 5 +- scripts/character/CharacterTemplate.cs | 2 +- scripts/character/Player.cs | 231 +++++++++--------- scripts/loader/sceneLoader/GameSceneLoader.cs | 25 +- scripts/weapon/WeaponTemplate.cs | 6 +- 9 files changed, 147 insertions(+), 128 deletions(-) diff --git a/locals/Log.csv b/locals/Log.csv index 096e251..b9250da 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -5,4 +5,7 @@ build_an_index,为{0}构建索引。,"Build an index for {0}." add_file_index,添加文件索引{0}。,"Add file index {0}." index_updated,{0}索引已更新。,"{0} Index has been updated." no_manifest_file,位于{0}的数据包,没有清单文件,无法加载。,"Packet located in {0}, no manifest file, cannot be loaded." -duplicate_at_path_id,位于{0}路径{1}的Id({2})已被占用,无法加载。,"Id({2}) in {0} path {1} is occupied and cannot be loaded." \ No newline at end of file +duplicate_at_path_id,位于{0}路径{1}的Id({2})已被占用,无法加载。,"Id({2}) in {0} path {1} is occupied and cannot be loaded." +duplicate_at_path_id,位于{0}路径{1}的Id({2})已被占用,无法加载。,"Id({2}) in {0} path {1} is occupied and cannot be loaded." +map_generator_is_not_set_up,未设置地图生成器。,Map generator is not set up. +map_generator_is_not_configured,地图生成器没有有效配置。,Map Generator is not configured. \ No newline at end of file diff --git a/locals/Log.en.translation b/locals/Log.en.translation index f17951b13406d16d0e2954e38d3c963ec8ca69b3..9e1a3983aefb3b1c0c3923128c75d135922f00ad 100644 GIT binary patch delta 186 zcmZo-yT`Ucnvu2CuqY#CvI3)lM{oV=YeSrH~9f92}kb3joMAeIGUHXxP<;-8b1nJ!xW^JTGN_}Seg t;$6~W$E2)gllPASEcVacSXzBUNy|b`9S-YhPC#KGUpY!F}t@)dwsZ}L1wZBa%b z3xtK07tA&XGE{+V)yc;glNDhy@>j0jl?HNTfHEM=2E_6}9JASoNsiI?*_>4`*6n+~ zciq!D8=lVI_k7CyXB`us&Rp@hp@{)a=-H-)&*pT3(A18Xt-U}!AVuszvp{wQ1qV3; E0QxOWMgRZ+ delta 96 zcmX@hewS^7G$X5FesW&QWCg}h&Hq5a#lXNI0>mITI$#BgD*&<5The node holder within the game scene /// 游戏场景内的节点持有者 /// -public class GameSceneNodeHolder +public static class GameSceneNodeHolder { /// /// Player instances within the game scene diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index d0b4d12..3b89ce8 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -32,7 +32,7 @@ public partial class CharacterTemplate : CharacterBody2D //Item containers are used to store items. //物品容器用于存储物品。 - protected IItemContainer? _itemContainer; + private IItemContainer? _itemContainer; public IItemContainer? ItemContainer diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index 6063f68..141b78c 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -1,64 +1,60 @@ -using Godot; using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; -using ColdMint.scripts; -using ColdMint.scripts.character; using ColdMint.scripts.damage; -using ColdMint.scripts.debug; using ColdMint.scripts.utils; using ColdMint.scripts.weapon; +using Godot; + +namespace ColdMint.scripts.character; /// /// 玩家角色 /// public partial class Player : CharacterTemplate { - private PackedScene FloatLabelPackedScene; + private PackedScene? _floatLabelPackedScene; - protected Control FloatLabel; + private Control? _floatLabel; //Empty object projectile //空的物品抛射线 - private Vector2[] emptyVector2Array = new[] { Vector2.Zero }; + private readonly Vector2[] _emptyVector2Array = new[] { Vector2.Zero }; //抛物线 - private Line2D Parabola; + private Line2D? _parabola; //用于检测玩家是否站在平台上的射线 - private RayCast2D PlatformDetectionRayCast2D; + private RayCast2D? _platformDetectionRayCast2D; //在拾捡范围内,可拾起的物品数量 - private int TotalNumberOfPickups = 0; + private int _totalNumberOfPickups; - private const float promptTextDistance = 50; + private const float PromptTextDistance = 50; //玩家可拾捡的物品 - private Node2D PickAbleItem; + private Node2D? _pickAbleItem; //抛出物品的飞行速度 - private float throwingVelocity = Config.CellSize * 13; + private float _throwingVelocity = Config.CellSize * 13; //射线是否与平台碰撞 - private bool CollidingWithPlatform = false; + private bool _collidingWithPlatform; //How long does it take for the character to recover from a collision with the platform after jumping off the platform (in seconds) //角色从平台上跳下后,多少时间后恢复与平台的碰撞(单位:秒) - private double PlatformCollisionRecoveryTime = 0.2f; + private double _platformCollisionRecoveryTime = 0.2f; //物品被扔出后多长时间恢复与地面和平台的碰撞(单位:秒) - private double ItemCollisionRecoveryTime = 0.045f; + private readonly double _itemCollisionRecoveryTime = 0.045f; public override void _Ready() { base._Ready(); CharacterName = TranslationServer.Translate("default_player_name"); - FloatLabelPackedScene = GD.Load("res://prefab/ui/FloatLabel.tscn"); - Parabola = GetNode("Parabola"); - PlatformDetectionRayCast2D = GetNode("PlatformDetectionRayCast"); + _floatLabelPackedScene = GD.Load("res://prefab/ui/FloatLabel.tscn"); + _parabola = GetNode("Parabola"); + _platformDetectionRayCast2D = GetNode("PlatformDetectionRayCast"); UpdateOperationTip(); GameSceneNodeHolder.HealthBarUi.MaxHp = MaxHp; GameSceneNodeHolder.HealthBarUi.CurrentHp = CurrentHp; @@ -71,16 +67,16 @@ public partial class Player : CharacterTemplate private void UpdateOperationTip() { var operationTipBuilder = new StringBuilder(); - if (TotalNumberOfPickups > 0) + if (_totalNumberOfPickups > 0) { //If there's anything around to pick up //如果周围有能捡的东西 if (CurrentItem == null) { - if (PickAbleItem != null) + if (_pickAbleItem != null) { - string name = null; - if (PickAbleItem is WeaponTemplate weaponTemplate) + string? name = null; + if (_pickAbleItem is WeaponTemplate weaponTemplate) { //When the weapon has no owner, a pick up prompt is displayed. //当武器没有主人时,显示捡起提示。 @@ -101,13 +97,13 @@ public partial class Player : CharacterTemplate } else { - string pickAbleItemName = null; - string currentItemName = null; + string? pickAbleItemName = null; + string? currentItemName = null; string mustBeThrown = TranslationServer.Translate("must_be_thrown"); - if (PickAbleItem != null) + if (_pickAbleItem != null) { //可捡的物品是武器 - if (PickAbleItem is WeaponTemplate weaponTemplate) + if (_pickAbleItem is WeaponTemplate weaponTemplate) { pickAbleItemName = TranslationServer.Translate(weaponTemplate.Name); } @@ -145,7 +141,7 @@ public partial class Player : CharacterTemplate operationTipBuilder.Append(' '); operationTipBuilder.Append(TranslationServer.Translate(InputMap.ActionGetEvents("ui_up")[0].AsText())); operationTipBuilder.Append(TranslationServer.Translate("jump")); - if (CollidingWithPlatform) + if (_collidingWithPlatform) { operationTipBuilder.Append(' '); operationTipBuilder.Append(TranslationServer.Translate(InputMap.ActionGetEvents("ui_down")[0].AsText())); @@ -177,11 +173,11 @@ public partial class Player : CharacterTemplate { //When the collision state between the platform detection ray and the platform changes //在平台检测射线与平台碰撞状态改变时 - if (PlatformDetectionRayCast2D.IsColliding() != CollidingWithPlatform) + if (_platformDetectionRayCast2D != null && _platformDetectionRayCast2D.IsColliding() != _collidingWithPlatform) { //When the state changes, update the action hint //当状态改变时,更新操作提示 - CollidingWithPlatform = PlatformDetectionRayCast2D.IsColliding(); + _collidingWithPlatform = _platformDetectionRayCast2D.IsColliding(); UpdateOperationTip(); } @@ -206,15 +202,15 @@ public partial class Player : CharacterTemplate //捡起物品 if (Input.IsActionJustPressed("pick_up")) { - var success = PickItem(PickAbleItem); + var success = PickItem(_pickAbleItem); if (success) { - PickAbleItem = null; - TotalNumberOfPickups--; - if (FloatLabel != null) + _pickAbleItem = null; + _totalNumberOfPickups--; + if (_floatLabel != null) { - FloatLabel.QueueFree(); - FloatLabel = null; + _floatLabel.QueueFree(); + _floatLabel = null; } UpdateOperationTip(); @@ -223,13 +219,13 @@ public partial class Player : CharacterTemplate if (Input.IsActionJustPressed("ui_down")) { - if (CollidingWithPlatform) + if (_collidingWithPlatform) { //When the character stands on the platform and presses the ui_down key, we cancel the collision between the character and the platform //当角色站在平台上按下 ui_down 键时,我们取消角色与平台的碰撞 var timer = new Timer(); AddChild(timer); - timer.WaitTime = PlatformCollisionRecoveryTime; + timer.WaitTime = _platformCollisionRecoveryTime; timer.OneShot = true; timer.Start(); timer.Timeout += () => @@ -246,15 +242,21 @@ public partial class Player : CharacterTemplate //抛出物品时,显示抛物线 if (Input.IsActionPressed("throw")) { - if (CurrentItem == null) + if (_parabola == null) { - Parabola.Points = emptyVector2Array; + return; } - else + + if (ItemMarker2D == null) { - Parabola.Points = - ParabolicUtils.ComputeParabolic(ItemMarker2D.Position, GetThrowVelocity(), Gravity, 0.1f); + //Cannot get the marked location of the item, then do not draw a line + //无法获取物品的标记位置,那么不绘制线 + return; } + + _parabola.Points = CurrentItem == null + ? _emptyVector2Array + : ParabolicUtils.ComputeParabolic(ItemMarker2D.Position, GetThrowVelocity(), Gravity, 0.1f); } @@ -262,15 +264,20 @@ public partial class Player : CharacterTemplate //抬起手时,抛出物品 if (Input.IsActionJustReleased("throw")) { - if (CurrentItem != null) + if (CurrentItem == null) return; + if (_parabola != null) { - Parabola.Points = new[] { Vector2.Zero }; - CurrentItem.Reparent(GameSceneNodeHolder.WeaponContainer); - if (CurrentItem is WeaponTemplate weaponTemplate) + _parabola.Points = new[] { Vector2.Zero }; + } + + CurrentItem.Reparent(GameSceneNodeHolder.WeaponContainer); + switch (CurrentItem) + { + case WeaponTemplate weaponTemplate: { var timer = new Timer(); weaponTemplate.AddChild(timer); - timer.WaitTime = ItemCollisionRecoveryTime; + timer.WaitTime = _itemCollisionRecoveryTime; timer.OneShot = true; timer.Timeout += () => { @@ -284,23 +291,20 @@ public partial class Player : CharacterTemplate timer.Start(); weaponTemplate.Sleeping = false; weaponTemplate.LinearVelocity = Vector2.Zero; + break; } - - if (CurrentItem is CharacterBody2D characterBody2D) - { + case CharacterBody2D characterBody2D: characterBody2D.Velocity = GetThrowVelocity(); - } - - if (CurrentItem is RigidBody2D rigidBody2D) - { + break; + case RigidBody2D rigidBody2D: rigidBody2D.LinearVelocity = GetThrowVelocity(); - } - - CurrentItem = null; - TotalNumberOfPickups++; - GameSceneNodeHolder.HotBar.RemoveItemFromItemSlotBySelectIndex(1); - UpdateOperationTip(); + break; } + + CurrentItem = null; + _totalNumberOfPickups++; + GameSceneNodeHolder.HotBar.RemoveItemFromItemSlotBySelectIndex(1); + UpdateOperationTip(); } } @@ -309,13 +313,18 @@ public partial class Player : CharacterTemplate { //We take the mouse position, normalize it, and then multiply it by the distance the player can throw //我们拿到鼠标的位置,将其归一化处理,然后乘以玩家可扔出的距离 - return GetLocalMousePosition().Normalized() * throwingVelocity; + return GetLocalMousePosition().Normalized() * _throwingVelocity; } public override void _Process(double delta) { AimTheCurrentItemAtAPoint(GetGlobalMousePosition()); - var itemMarker2DPosition = ItemMarker2D.Position; + var itemMarker2DPosition = Vector2.Zero; + if (ItemMarker2D != null) + { + itemMarker2DPosition = ItemMarker2D.Position; + } + var axis = Input.GetAxis("ui_left", "ui_right"); switch (axis) { @@ -323,23 +332,30 @@ public partial class Player : CharacterTemplate //Minus 1, we move to the left //-1,向左移动 FacingLeft = true; - itemMarker2DPosition.X = -ReadOnlyItemMarkerOriginalX; + if (ItemMarker2D != null) + { + itemMarker2DPosition.X = -ReadOnlyItemMarkerOriginalX; + } + Flip(); break; case 1: //1, move to the right //1,向右移动 FacingLeft = false; - itemMarker2DPosition.X = ReadOnlyItemMarkerOriginalX; + if (ItemMarker2D != null) + { + itemMarker2DPosition.X = ReadOnlyItemMarkerOriginalX; + } + Flip(); break; - default: - //0, when it's not pressed - //0,没有按下时 - break; } - ItemMarker2D.Position = itemMarker2DPosition; + if (ItemMarker2D != null) + { + ItemMarker2D.Position = itemMarker2DPosition; + } } protected override void Flip() @@ -347,12 +363,9 @@ public partial class Player : CharacterTemplate base.Flip(); //If there is a weapon, flip it too //如果有武器的话,也要翻转 - if (CurrentItem != null) + if (CurrentItem is WeaponTemplate weapon) { - if (CurrentItem is WeaponTemplate weapon) - { - weapon.Flip(FacingLeft); - } + weapon.Flip(FacingLeft); } } @@ -365,50 +378,42 @@ public partial class Player : CharacterTemplate return; } - if (node is not Node2D) + if (node is not Node2D node2D) { return; } - var node2D = node as Node2D; - TotalNumberOfPickups++; - PickAbleItem = node2D; - if (FloatLabel != null) + _totalNumberOfPickups++; + _pickAbleItem = node2D; + if (_floatLabelPackedScene != null) { - FloatLabel.QueueFree(); - } - - FloatLabel = (Control)FloatLabelPackedScene.Instantiate(); - var rotationDegreesNode2D = node2D.RotationDegrees; - var rotationDegreesNode2DAbs = Math.Abs(rotationDegreesNode2D); - if (rotationDegreesNode2DAbs > 90) - { - FloatLabel.Position = new Vector2(0, promptTextDistance); - } - else - { - FloatLabel.Position = new Vector2(0, -promptTextDistance); - } - - FloatLabel.RotationDegrees = 0 - rotationDegreesNode2D; - var label = FloatLabel.GetNode