Browse Source

build:add BirthPoint.cs and ObjOfCharacter.cs

tags/0.1.0
shangfengh 3 years ago
parent
commit
dd9fb93ddb
4 changed files with 84 additions and 26 deletions
  1. +22
    -21
      logic/GameClass/GameObj/GameObj.cs
  2. +29
    -0
      logic/GameClass/GameObj/Map/BirthPoint.cs
  3. +28
    -0
      logic/GameClass/GameObj/ObjOfCharacter.cs
  4. +5
    -5
      logic/Preparation/GameData/GameData.cs

+ 22
- 21
logic/GameClass/GameObj/GameObj.cs View File

@@ -20,8 +20,8 @@ namespace GameClass.GameObj
private GameObjType type; private GameObjType type;
public GameObjType Type => 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 const long noneID = long.MinValue;
public long ID { get; } public long ID { get; }


@@ -29,8 +29,8 @@ namespace GameClass.GameObj
public XY Position public XY Position
{ {
get => position; get => position;
protected set
{
protected
set {
lock (gameObjLock) lock (gameObjLock)
{ {
position = value; position = value;
@@ -39,12 +39,11 @@ namespace GameClass.GameObj
} }
public abstract bool IsRigid { get; } public abstract bool IsRigid { get; }


private XY facingDirection = new(1,0);
private XY facingDirection = new(1, 0);
public XY FacingDirection public XY FacingDirection
{ {
get => facingDirection; get => facingDirection;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
facingDirection = value; facingDirection = value;
} }
@@ -55,8 +54,7 @@ namespace GameClass.GameObj
public bool CanMove public bool CanMove
{ {
get => canMove; get => canMove;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
{ {
canMove = value; canMove = value;
@@ -68,8 +66,7 @@ namespace GameClass.GameObj
public bool IsMoving public bool IsMoving
{ {
get => isMoving; get => isMoving;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
{ {
isMoving = value; isMoving = value;
@@ -81,23 +78,21 @@ namespace GameClass.GameObj
public bool IsResetting public bool IsResetting
{ {
get => isResetting; get => isResetting;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
{ {
isResetting = value; isResetting = value;
} }
} }
} }
public bool IsAvailable => !IsMoving && CanMove && !IsResetting; //是否能接收指令
public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令
public int Radius { get; } public int Radius { get; }


private PlaceType place; private PlaceType place;
public PlaceType Place public PlaceType Place
{ {
get => place; get => place;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
{ {
place = value; place = value;
@@ -111,8 +106,7 @@ namespace GameClass.GameObj
public int MoveSpeed public int MoveSpeed
{ {
get => moveSpeed; get => moveSpeed;
set
{
set {
lock (gameObjLock) lock (gameObjLock)
{ {
moveSpeed = value; moveSpeed = value;
@@ -123,7 +117,14 @@ namespace GameClass.GameObj
/// 原初移动速度 /// 原初移动速度
/// </summary> /// </summary>
private int orgMoveSpeed; 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) public long Move(XY moveVec)
@@ -158,7 +159,7 @@ namespace GameClass.GameObj
{ {
lock (gameObjLock) lock (gameObjLock)
{ {
facingDirection = new XY(1,0);
facingDirection = new XY(1, 0);
isMoving = false; isMoving = false;
canMove = false; canMove = false;
isResetting = true; isResetting = true;
@@ -169,7 +170,7 @@ namespace GameClass.GameObj
/// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable
/// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现 /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现
/// </summary> /// </summary>
/// <returns> 依具体类及该方法参数而定,默认为false </returns>
/// <returns> 依具体类及该方法参数而定,默认为false </returns>
protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false; protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj); bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj);
public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType) public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType)


+ 29
- 0
logic/GameClass/GameObj/Map/BirthPoint.cs View File

@@ -0,0 +1,29 @@
using Preparation.Interface;
using Preparation.Utility;
using Preparation.GameData;

namespace GameClass.GameObj
{
/// <summary>
/// 出生点
/// </summary>
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;
}
}

+ 28
- 0
logic/GameClass/GameObj/ObjOfCharacter.cs View File

@@ -0,0 +1,28 @@
using Preparation.Interface;
using Preparation.Utility;

namespace GameClass.GameObj
{
/// <summary>
/// 所有物,具有主人(Parent)(特定玩家)属性的对象
/// </summary>
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)
{
}
}
}

+ 5
- 5
logic/Preparation/GameData/GameData.cs View File

@@ -16,20 +16,20 @@ namespace Preparation.GameData
public const int MinSpeed = 1; // 最小速度 public const int MinSpeed = 1; // 最小速度
public const int MaxSpeed = int.MaxValue; // 最大速度 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; return ret;
} }
public static int PosGridToCellX(XYPosition pos) // 求坐标所在的格子的x坐标
public static int PosGridToCellX(XY pos) // 求坐标所在的格子的x坐标
{ {
return pos.x / numOfPosGridPerCell; return pos.x / numOfPosGridPerCell;
} }
public static int PosGridToCellY(XYPosition pos) // 求坐标所在的格子的y坐标
public static int PosGridToCellY(XY pos) // 求坐标所在的格子的y坐标
{ {
return pos.y / numOfPosGridPerCell; 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); return PosGridToCellX(pos1) == PosGridToCellX(pos2) && PosGridToCellY(pos1) == PosGridToCellY(pos2);
} }


Loading…
Cancel
Save