Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

chore: Add abstract class GameObj and MoveableObj #5

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions logic/Preparation/GameObj/GameObj.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Preparation.Interface;
using Preparation.Utility;

namespace Preparation.GameObj
{
public abstract class GameObj:IGameObj
{
public enum GameObjType
{
Character = 0,
Obj = 1
}
protected readonly object gameObjLock = new object();
protected readonly XYPosition birthPos;

public long ID { get;}

private XYPosition position;
public XYPosition Position
{
get => position;
protected set
{
lock (gameObjLock)
{
position = value;
}
}
}
public abstract bool IsRigid { get; }

private double facingDirection = 0;
public double FacingDirection
{
get => facingDirection;
set
{
lock (gameObjLock)
facingDirection = value;
}
}
public abstract ShapeType Shape { get; }

private bool canMove;
public bool CanMove
{
get => canMove;
set
{
lock(gameObjLock)
{
canMove = value;
}
}
}

private bool isMoving;
public bool IsMoving
{
get => isMoving;
set
{
lock(gameObjLock)
{
isMoving = value;
}
}
}

private bool isResetting;
public bool IsResetting
{
get => isResetting;
set
{
lock(gameObjLock)
{
isResetting = value;
}
}
}
public bool IsAvailable => !IsMoving && CanMove && !IsResetting; //是否能接收指令
public int Radius { get; }

private PlaceType place;
public PlaceType Place
{
get => place;
set
{
lock(gameObjLock)
{
place = value;
}
}
}

public virtual bool CanSee(GameObj obj)
{
if (obj.Place == PlaceType.Invisible) //先判断是否隐身
return false;
if (obj.Place == PlaceType.Land)
return true;
if (obj.Place == this.Place)
return true;
return false;
}

public GameObj(XYPosition initPos,int initRadius,PlaceType initPlace)
{
this.birthPos = initPos;
this.Radius = initRadius;
this.place = initPlace;
}
}
}
52 changes: 52 additions & 0 deletions logic/Preparation/GameObj/MoveableObj.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Preparation.Interface;
using Preparation.Utility;

namespace Preparation.GameObj
{
public abstract class MoveableObj: GameObj,IMoveable
{
private int moveSpeed;
public int MoveSpeed
{
get => moveSpeed;
set
{
lock(gameObjLock)
{
moveSpeed = value;
}
}
}
protected readonly object moveLock = new object();
public override ShapeType Shape => ShapeType.Circle; //会移动的一定是圆形
public MoveableObj(XYPosition initPos, int initRadius, PlaceType initPlace,int initSpeed):base(initPos,initRadius,initPlace)
{
moveSpeed = initSpeed;
}
public long Move(Vector moveVec)
{
var XYVec = Vector.Vector2XY(moveVec);
lock (moveLock)
{
FacingDirection = moveVec.angle;
this.Position += XYVec;
}
return (long)(XYVec.ToVector2()* new Vector2(0, 0));
}

public virtual void Reset() //复活时数据重置
{
lock (moveLock)
{
this.Position = birthPos;
}
lock(gameObjLock)
{
FacingDirection = 0.0;
IsMoving = false;
CanMove = false;
IsResetting = true;
}
}
}
}
1 change: 0 additions & 1 deletion logic/Preparation/Interface/IGameObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public interface IGameObj
public bool IsResetting { get; set; } //reviving
public bool IsAvailable { get; }
public int Radius { get; } //if Square, Radius equals half length of one side
public object MoveLock { get; }
public PlaceType Place { get; set; }

}
Expand Down
8 changes: 8 additions & 0 deletions logic/Preparation/Preparation.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions logic/Preparation/Utility/XYPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ public static double Distance(XYPosition p1,XYPosition p2)
{
return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
public Vector2 ToVector2()
{
return new Vector2(this.x, this.y);
}
}
}
2 changes: 1 addition & 1 deletion logic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@

## 开发人员

+ ……(自己加)
+ 唐昌礼,张鹤龄,刘浩然

4 changes: 4 additions & 0 deletions logic/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="IGameObj.cs" />
</ItemGroup>

</Project>
16 changes: 14 additions & 2 deletions logic/logic.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{5B5FD8F7-1B73-4189-8AEF-48B3ED077ADB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{5B5FD8F7-1B73-4189-8AEF-48B3ED077ADB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{3B18D60F-EECF-4E5D-B5FF-6A8E29422335}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{3B18D60F-EECF-4E5D-B5FF-6A8E29422335}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Preparation", "Preparation\Preparation.csproj", "{84B5D876-B6DE-499C-9EF2-D924FDC984B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameEngine", "GameEngine\GameEngine.csproj", "{C1CEF591-E65C-4E16-A369-F5F9EA46F978}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{3B18D60F-EECF-4E5D-B5FF-6A8E29422335}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B18D60F-EECF-4E5D-B5FF-6A8E29422335}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B18D60F-EECF-4E5D-B5FF-6A8E29422335}.Release|Any CPU.Build.0 = Release|Any CPU
{84B5D876-B6DE-499C-9EF2-D924FDC984B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84B5D876-B6DE-499C-9EF2-D924FDC984B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84B5D876-B6DE-499C-9EF2-D924FDC984B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84B5D876-B6DE-499C-9EF2-D924FDC984B2}.Release|Any CPU.Build.0 = Release|Any CPU
{C1CEF591-E65C-4E16-A369-F5F9EA46F978}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1CEF591-E65C-4E16-A369-F5F9EA46F978}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1CEF591-E65C-4E16-A369-F5F9EA46F978}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1CEF591-E65C-4E16-A369-F5F9EA46F978}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down