Skip to content

Commit

Permalink
[*] fixed player mover bug
Browse files Browse the repository at this point in the history
[+] add Collision System Editor
  • Loading branch information
JiepengTan committed Aug 22, 2019
1 parent 1a1565b commit 9d39f9a
Show file tree
Hide file tree
Showing 21 changed files with 330 additions and 152 deletions.
13 changes: 12 additions & 1 deletion Unity/Assets/LockstepEngine/Collision2D/CRigidbody.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Lockstep.Collision2D;
using Lockstep.Logging;
using Lockstep.Math;

namespace Lockstep.Logic {
public delegate void OnFloorResultCallback(bool isOnFloor);

[Serializable]
public class CRigidbody {
public CTransform2D transform { get; set; }
public CTransform2D transform { get; private set; }
public static LFloat G = new LFloat(10);
public static LFloat MinSleepSpeed = new LFloat(true, 100);
public static LFloat FloorFriction = new LFloat(20);
Expand All @@ -21,9 +22,18 @@ public class CRigidbody {
public bool isEnable = true;
public bool isSleep = false;
public bool isOnFloor;

public void Init(CTransform2D transform2D){
this.transform = transform2D;
}

//private int __id;
//private static int __idCount;
public void DoStart(){
//__id = __idCount++;
LFloat y = LFloat.zero;
isOnFloor = TestOnFloor(transform.Pos3, ref y);
Speed = LVector3.zero;
isSleep = isOnFloor;
}

Expand Down Expand Up @@ -74,6 +84,7 @@ public void DoUpdate(LFloat deltaTime){
public void AddImpulse(LVector3 force){
isSleep = false;
Speed += force / Mass;
//Debug.Log(__id+ " AddImpulse " + force +" after " + Speed);
}
public void ResetSpeed(LFloat ySpeed){
Speed = LVector3.zero;
Expand Down
17 changes: 10 additions & 7 deletions Unity/Assets/LockstepEngine/Collision2D/CollisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class CollisionSystem : ICollisionSystem {
private Dictionary<int, ColliderProxy> id2Proxy = new Dictionary<int, ColliderProxy>();
private HashSet<long> _curPairs = new HashSet<long>();
private HashSet<long> _prePairs = new HashSet<long>();
public const int LayerCount = 32;
public const int MaxLayerCount = 32;
private List<ColliderProxy> tempLst = new List<ColliderProxy>();

public FuncGlobalOnTriggerEvent funcGlobalOnTriggerEvent;
Expand All @@ -32,9 +32,11 @@ public ColliderProxy GetCollider(int id){


public int[] AllTypes;
public int[][] InterestingMasks;
public bool[] InterestingMasks;
public int LayerCount;

public void DoStart(int[][] interestingMasks, int[] allTypes){
public void DoStart(bool[] interestingMasks, int[] allTypes){
LayerCount = allTypes.Length;
this.InterestingMasks = interestingMasks;
this.AllTypes = allTypes;
//init _collisionMask//TODO read from file
Expand Down Expand Up @@ -151,10 +153,11 @@ public void DoUpdate(LFloat deltaTime){
foreach (var val in tempLst) {
val.IsMoved = false;
var bound = val.GetBounds();
var targetLayers = InterestingMasks[val.LayerType];
foreach (var layerType in targetLayers) {
var boundsTree = GetBoundTree(layerType);
boundsTree.CheckCollision(val, bound);
for (int i = 0; i < LayerCount; i++) {
if (InterestingMasks[val.LayerType * LayerCount + i]) {
var boundsTree = GetBoundTree(i);
boundsTree.CheckCollision(val, bound);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Lockstep.Collision2D {
public delegate void FuncCollision(ColliderProxy obj);

public interface ICollisionSystem {
void DoStart(int[][] interestingMasks, int[] allTypes);
void DoStart(bool[] interestingMasks, int[] allTypes);
void DoUpdate(LFloat deltaTime);
ColliderProxy GetCollider(int id);
void AddCollider(ColliderProxy collider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public class TestQuadTree : MonoBehaviour {
public float percent = 0.1f;
public int count = 100;

public int[][] InterestingMasks = new int[][] {
new int[] { },
new int[] { },
new int[] {0, 1}
};
public bool[] InterestingMasks;

private int[] allTypes = new int[] {0, 1, 2};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private void Start(){
CRigidbody = new CRigidbody();
CTransform2D = new CTransform2D();
CTransform2D.Pos3 = transform.position.ToLVector3();
CRigidbody.transform = CTransform2D;
CRigidbody.Init(CTransform2D);
CRigidbody.DoStart();
}

Expand Down
Binary file modified Unity/Assets/Resources/Prefabs/Enemy.prefab
Binary file not shown.
Binary file modified Unity/Assets/Resources/Prefabs/Player.prefab
Binary file not shown.
Binary file modified Unity/Assets/Scenes/Demo.unity
Binary file not shown.
2 changes: 1 addition & 1 deletion Unity/Assets/Scripts/Core/Base/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class BaseEntity : BaseLifeCycle, IEntity, ILPTriggerEventHandler {

public BaseEntity(){
Debug.Trace("BaseEntity " + IdCounter.ToString(), true);
rigidbody.transform = transform;
}

protected void RegisterComponent(BaseComponent comp){
Expand All @@ -30,6 +29,7 @@ protected void RegisterComponent(BaseComponent comp){
}

public override void DoAwake(){
rigidbody.Init(transform);
EntityId = IdCounter++;
foreach (var comp in allComponents) {
comp.DoAwake();
Expand Down
6 changes: 4 additions & 2 deletions Unity/Assets/Scripts/Logic/Config/GameConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ public void CopyTo(object dst){
FieldInfo[] fields = dst.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
foreach (var field in fields) {
var type = field.FieldType;
if (typeof(BaseComponent).IsAssignableFrom(type)) {
if (typeof(BaseComponent).IsAssignableFrom(type)
|| typeof(CRigidbody).IsAssignableFrom(type)
|| typeof(Transform).IsAssignableFrom(type)
) {
CopyTo(field.GetValue(dst), field.GetValue(Entity));
}
else {
field.SetValue(dst, field.GetValue(Entity));
}
}

}

void CopyTo(object dst, object src){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public class CBrain : Component {
public LFloat atkInterval = 1;
private LFloat atkTimer;

public override void DoUpdate(LFloat deltaTime){
public override void DoUpdate(LFloat deltaTime){
if (!entity.rigidbody.isOnFloor) {
return;
}
//find target
var allPlayer = GameManager.allPlayers;
var minDist = LFloat.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public override void BindEntity(BaseEntity entity){
}

public override void DoUpdate(LFloat deltaTime){
//if (!entity.rigidbody.isOnFloor) {
// return;
//}
if (!entity.rigidbody.isOnFloor) {
return;
}

var needChase = input.inputUV.sqrMagnitude > new LFloat(true, 10);
if (needChase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public bool Fire(int idx){
return false;
}

Debug.Log("TryFire " + idx);
//Debug.Log("TryFire " + idx);

if (isFiring) return false; //
var skill = skills[idx];
Expand Down
2 changes: 1 addition & 1 deletion Unity/Assets/Scripts/Logic/EntityComponent/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void StopSkill(int idx = -1){
}

public virtual void TakeDamage(int amount, LVector3 hitPoint){
if (isDead) return;
if (isInvincible || isDead) return;
curHealth -= amount;
EntityView?.OnTakeDamage(amount, hitPoint);
OnTakeDamage(amount, hitPoint);
Expand Down
Loading

0 comments on commit 9d39f9a

Please sign in to comment.