From dd9fb93ddbf55ef8b469c66a9cea98ecbf22c1e9 Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Sat, 29 Oct 2022 21:54:38 +0800 Subject: [PATCH] build:add BirthPoint.cs and ObjOfCharacter.cs --- logic/GameClass/GameObj/GameObj.cs | 43 ++++++++++++----------- logic/GameClass/GameObj/Map/BirthPoint.cs | 29 +++++++++++++++ logic/GameClass/GameObj/ObjOfCharacter.cs | 28 +++++++++++++++ logic/Preparation/GameData/GameData.cs | 10 +++--- 4 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 logic/GameClass/GameObj/Map/BirthPoint.cs create mode 100644 logic/GameClass/GameObj/ObjOfCharacter.cs diff --git a/logic/GameClass/GameObj/GameObj.cs b/logic/GameClass/GameObj/GameObj.cs index 8164961..27f2abd 100644 --- a/logic/GameClass/GameObj/GameObj.cs +++ b/logic/GameClass/GameObj/GameObj.cs @@ -20,8 +20,8 @@ namespace GameClass.GameObj private GameObjType type; public GameObjType Type => type; - private static long currentMaxID = 0; //目前游戏对象的最大ID - public const long invalidID = long.MaxValue; //无效的ID + private static long currentMaxID = 0; // 目前游戏对象的最大ID + public const long invalidID = long.MaxValue; // 无效的ID public const long noneID = long.MinValue; public long ID { get; } @@ -29,8 +29,8 @@ namespace GameClass.GameObj public XY Position { get => position; - protected set - { + protected + set { lock (gameObjLock) { position = value; @@ -39,12 +39,11 @@ namespace GameClass.GameObj } public abstract bool IsRigid { get; } - private XY facingDirection = new(1,0); + private XY facingDirection = new(1, 0); public XY FacingDirection { get => facingDirection; - set - { + set { lock (gameObjLock) facingDirection = value; } @@ -55,8 +54,7 @@ namespace GameClass.GameObj public bool CanMove { get => canMove; - set - { + set { lock (gameObjLock) { canMove = value; @@ -68,8 +66,7 @@ namespace GameClass.GameObj public bool IsMoving { get => isMoving; - set - { + set { lock (gameObjLock) { isMoving = value; @@ -81,23 +78,21 @@ namespace GameClass.GameObj public bool IsResetting { get => isResetting; - set - { + set { lock (gameObjLock) { isResetting = value; } } } - public bool IsAvailable => !IsMoving && CanMove && !IsResetting; //是否能接收指令 + public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令 public int Radius { get; } private PlaceType place; public PlaceType Place { get => place; - set - { + set { lock (gameObjLock) { place = value; @@ -111,8 +106,7 @@ namespace GameClass.GameObj public int MoveSpeed { get => moveSpeed; - set - { + set { lock (gameObjLock) { moveSpeed = value; @@ -123,7 +117,14 @@ namespace GameClass.GameObj /// 原初移动速度 /// private int orgMoveSpeed; - public int OrgMoveSpeed { get => orgMoveSpeed; protected set { orgMoveSpeed = value; } } + public int OrgMoveSpeed + { + get => orgMoveSpeed; + protected + set { + orgMoveSpeed = value; + } + } // 移动,改变坐标 public long Move(XY moveVec) @@ -158,7 +159,7 @@ namespace GameClass.GameObj { lock (gameObjLock) { - facingDirection = new XY(1,0); + facingDirection = new XY(1, 0); isMoving = false; canMove = false; isResetting = true; @@ -169,7 +170,7 @@ namespace GameClass.GameObj /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现 /// - /// 依具体类及该方法参数而定,默认为false + /// 依具体类及该方法参数而定,默认为false protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false; bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj); public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType) diff --git a/logic/GameClass/GameObj/Map/BirthPoint.cs b/logic/GameClass/GameObj/Map/BirthPoint.cs new file mode 100644 index 0000000..3d4ab37 --- /dev/null +++ b/logic/GameClass/GameObj/Map/BirthPoint.cs @@ -0,0 +1,29 @@ +using Preparation.Interface; +using Preparation.Utility; +using Preparation.GameData; + +namespace GameClass.GameObj +{ + /// + /// 出生点 + /// + public class BirthPoint : ObjOfCharacter + { + public BirthPoint(XY initPos) : + base(initPos, GameData.numOfPosGridPerCell / 2, PlaceType.Land, GameObjType.BirthPoint) + { + this.CanMove = false; + } + // 修改建议:需要避免非自己的玩家进入出生点,否则会重叠 + public override bool IsRigid => true; + protected override bool IgnoreCollideExecutor(IGameObj targetObj) + { + if (targetObj.Type != GameObjType.Character) + return true; // 非玩家不碰撞 + else if (targetObj.Type == GameObjType.Character && targetObj.ID == this.Parent.ID) + return true; // 出生点所属的玩家不碰撞 + return false; + } + public override ShapeType Shape => ShapeType.Square; + } +} \ No newline at end of file diff --git a/logic/GameClass/GameObj/ObjOfCharacter.cs b/logic/GameClass/GameObj/ObjOfCharacter.cs new file mode 100644 index 0000000..a09c6e5 --- /dev/null +++ b/logic/GameClass/GameObj/ObjOfCharacter.cs @@ -0,0 +1,28 @@ +using Preparation.Interface; +using Preparation.Utility; + +namespace GameClass.GameObj +{ + /// + /// 所有物,具有主人(Parent)(特定玩家)属性的对象 + /// + public abstract class ObjOfCharacter : GameObj, IObjOfCharacter + { + private ICharacter? parent = null; // 主人 + public ICharacter? Parent + { + get => parent; + set { + lock (gameObjLock) + { + parent = value; + } + } + } + // LHR注:本来考虑在构造函数里设置parent属性,见THUAI4在游戏引擎中才设置该属性,作罢。——2021/9/24 + public ObjOfCharacter(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType) : + base(initPos, initRadius, initPlace, initType) + { + } + } +} diff --git a/logic/Preparation/GameData/GameData.cs b/logic/Preparation/GameData/GameData.cs index 2d13d58..0903b6d 100644 --- a/logic/Preparation/GameData/GameData.cs +++ b/logic/Preparation/GameData/GameData.cs @@ -16,20 +16,20 @@ namespace Preparation.GameData public const int MinSpeed = 1; // 最小速度 public const int MaxSpeed = int.MaxValue; // 最大速度 - public static XYPosition GetCellCenterPos(int x, int y) // 求格子的中心坐标 + public static XY GetCellCenterPos(int x, int y) // 求格子的中心坐标 { - XYPosition ret = new((x * numOfPosGridPerCell) + (numOfPosGridPerCell / 2), (y * numOfPosGridPerCell) + (numOfPosGridPerCell / 2)); + XY ret = new((x * numOfPosGridPerCell) + (numOfPosGridPerCell / 2), (y * numOfPosGridPerCell) + (numOfPosGridPerCell / 2)); return ret; } - public static int PosGridToCellX(XYPosition pos) // 求坐标所在的格子的x坐标 + public static int PosGridToCellX(XY pos) // 求坐标所在的格子的x坐标 { return pos.x / numOfPosGridPerCell; } - public static int PosGridToCellY(XYPosition pos) // 求坐标所在的格子的y坐标 + public static int PosGridToCellY(XY pos) // 求坐标所在的格子的y坐标 { return pos.y / numOfPosGridPerCell; } - public static bool IsInTheSameCell(XYPosition pos1, XYPosition pos2) + public static bool IsInTheSameCell(XY pos1, XY pos2) { return PosGridToCellX(pos1) == PosGridToCellX(pos2) && PosGridToCellY(pos1) == PosGridToCellY(pos2); }