Browse Source

refactor: Change Vector and XYPosition to XY

tags/0.1.0
shangfengh 3 years ago
parent
commit
fe7d296bb9
5 changed files with 42 additions and 47 deletions
  1. +2
    -2
      logic/Preparation/Interface/IGameObj.cs
  2. +1
    -1
      logic/Preparation/Interface/IMap.cs
  3. +3
    -3
      logic/Preparation/Interface/IMoveable.cs
  4. +0
    -41
      logic/Preparation/Utility/Vector.cs
  5. +36
    -0
      logic/Preparation/Utility/XY.cs

+ 2
- 2
logic/Preparation/Interface/IGameObj.cs View File

@@ -6,8 +6,8 @@ namespace Preparation.Interface
{
public GameObjType Type { get; }
public long ID { get; }
public XYPosition Position { get; } // if Square, Pos equals the center
public double FacingDirection { get; }
public XY Position { get; } // if Square, Pos equals the center
public XY FacingDirection { get; }
public bool IsRigid { get; }
public ShapeType Shape { get; }
public bool CanMove { get; set; }


+ 1
- 1
logic/Preparation/Interface/IMap.cs View File

@@ -13,6 +13,6 @@ namespace Preparation.Interface
Dictionary<GameObjIdx, ReaderWriterLockSlim> GameObjLockDict { get; }

public bool IsOutOfBound(IGameObj obj);
public IOutOfBound GetOutOfBound(XYPosition pos); // 返回新建的一个OutOfBound对象
public IOutOfBound GetOutOfBound(XY pos); // 返回新建的一个OutOfBound对象
}
}

+ 3
- 3
logic/Preparation/Interface/IMoveable.cs View File

@@ -7,9 +7,9 @@ namespace Preparation.Interface
{
object MoveLock { get; }
public int MoveSpeed { get; }
public long Move(Vector moveVec);
public long Move(XY moveVec);
protected bool IgnoreCollide(IGameObj targetObj); // 忽略碰撞,在具体类中实现
public bool WillCollideWith(IGameObj? targetObj, XYPosition nextPos) // 检查下一位置是否会和目标物碰撞
public bool WillCollideWith(IGameObj? targetObj, XY nextPos) // 检查下一位置是否会和目标物碰撞
{
if (targetObj == null)
return false;
@@ -20,7 +20,7 @@ namespace Preparation.Interface
return false;
if (targetObj.Shape == ShapeType.Circle)
{
return XYPosition.Distance(nextPos, targetObj.Position) < targetObj.Radius + Radius;
return XY.Distance(nextPos, targetObj.Position) < targetObj.Radius + Radius;
}
else // Square
{


+ 0
- 41
logic/Preparation/Utility/Vector.cs View File

@@ -1,41 +0,0 @@
using System;

namespace Preparation.Utility
{
public class Vector
{
public int x;
public int y;
public Vector(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return "(" + x.ToString() + "," + y.ToString() + ")";
}
public static int operator*(Vector v1, Vector v2)
{
return (v1.x * v2.x) + (v1.y * v2.y);
}
public static Vector operator +(Vector v1, Vector v2)
{
return new Vector(v1.x + v2.x, v1.y + v2.y);
}
public static Vector operator -(Vector v1, Vector v2)
{
return new Vector(v1.x - v2.x, v1.y - v2.y);
}
}
public class XYPosition: Vector
{
public XYPosition(int x, int y):base(x,y){}
public static double Distance(XYPosition p1, XYPosition p2)
{
return Math.Sqrt(((long)(p1.x - p2.x) * (p1.x - p2.x)) + ((long)(p1.y - p2.y) * (p1.y - p2.y)));
}
}
}

+ 36
- 0
logic/Preparation/Utility/XY.cs View File

@@ -0,0 +1,36 @@
using System;

namespace Preparation.Utility
{

public struct XY
{
public int x;
public int y;
public XY(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return "(" + x.ToString() + "," + y.ToString() + ")";
}
public static int operator *(XY v1, XY v2)
{
return (v1.x * v2.x) + (v1.y * v2.y);
}
public static XY operator +(XY v1, XY v2)
{
return new XY(v1.x + v2.x, v1.y + v2.y);
}
public static XY operator -(XY v1, XY v2)
{
return new XY(v1.x - v2.x, v1.y - v2.y);
}
public static double Distance(XY p1, XY p2)
{
return Math.Sqrt(((long)(p1.x - p2.x) * (p1.x - p2.x)) + ((long)(p1.y - p2.y) * (p1.y - p2.y)));
}
}
}

Loading…
Cancel
Save