Skip to content

Commit

Permalink
[+] add basic enemy AI
Browse files Browse the repository at this point in the history
[+] add BaseActor
[+] import plugin FloatBar
[+] import plugin FloatText
  • Loading branch information
JiepengTan committed Aug 21, 2019
1 parent 47ee0f8 commit e40520c
Show file tree
Hide file tree
Showing 55 changed files with 1,040 additions and 96 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
2. [简单服务器][13]
3. [基本位置同步][13]
4. 不同步的检测与定位
5. 逻辑的基本同步技能
5. 逻辑的基本同步
6. 碰撞检测库的使用
7. 添加技能

Expand Down
13 changes: 10 additions & 3 deletions Unity/Assets/LockstepEngine/Math/BaseType/LFloat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,27 @@ public override string ToString(){
#endregion

#region override type convert
public static implicit operator LFloat(short value){
return new LFloat(true,value * Precision);
}

public static explicit operator LFloat(int value){
public static explicit operator short(LFloat value){
return (short)(value._val / Precision);
}

public static implicit operator LFloat(int value){
return new LFloat(true,value * Precision);
}

public static explicit operator int(LFloat value){
public static implicit operator int(LFloat value){
return value._val / Precision;
}

public static explicit operator LFloat(long value){
return new LFloat(true,value * Precision);
}

public static explicit operator long(LFloat value){
public static implicit operator long(LFloat value){
return value._val / Precision;
}

Expand Down
Binary file modified Unity/Assets/Scenes/Demo.unity
Binary file not shown.
4 changes: 1 addition & 3 deletions Unity/Assets/Scripts/Core/Base/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public class BaseEntity : BaseLifeCycle, IEntity, ILPTriggerEventHandler {
public CTransform2D transform = new CTransform2D();

public int EntityId;
public int PrefabId;
public object engineTransform;
protected List<BaseComponent> allComponents = new List<BaseComponent>();
public LFloat speed = new LFloat(5);
public int currentHealth;
public int PrefabId;

public BaseEntity(){
Debug.Trace("BaseEntity " + IdCounter.ToString(), true);
Expand Down
4 changes: 2 additions & 2 deletions Unity/Assets/Scripts/Logic/Component/CMover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace LockstepTutorial {
public partial class CMover : BasePlayerComponent {
static LFloat _sqrStopDist = new LFloat(true, 40);
public LFloat speed => entity.speed;
public LFloat speed => player.moveSpd;
public bool hasReachTarget = false;
public bool needMove = true;

Expand All @@ -18,7 +18,7 @@ public override void DoUpdate(LFloat deltaTime){
var dir = input.inputUV.normalized;
transform.pos = transform.pos + dir * speed * deltaTime;
var targetDeg = dir.ToDeg();
transform.deg = CTransform2D.TurnToward(targetDeg, transform.deg, 360 * deltaTime, out var hasReachDeg);
transform.deg = CTransform2D.TurnToward(targetDeg, transform.deg, player.turnSpd * deltaTime, out var hasReachDeg);
}

hasReachTarget = !needChase;
Expand Down
34 changes: 34 additions & 0 deletions Unity/Assets/Scripts/Logic/Entity/BaseActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Lockstep.Logging;
using Lockstep.Logic;
using Lockstep.Math;

namespace LockstepTutorial {
public partial class BaseActor : BaseEntity {
public IActorView actorView;
public LFloat moveSpd = 5;
public LFloat turnSpd = 360;
public int curHealth;
public int maxHealth = 100;
public bool isDead => curHealth <= 0;

public BaseActor(){
curHealth = maxHealth;
}

public virtual void TakeDamage(int amount, LVector3 hitPoint){
if (isDead) return;
curHealth -= amount;
actorView?.OnTakeDamage(amount, hitPoint);
OnTakeDamage(amount, hitPoint);
if (isDead) {
actorView?.OnDead();
OnDead();
}
}

protected virtual void OnTakeDamage(int amount, LVector3 hitPoint){}

protected virtual void OnDead(){}
}
}
3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/Logic/Entity/BaseActor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 64 additions & 3 deletions Unity/Assets/Scripts/Logic/Entity/Enemy.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
using Lockstep.Collision2D;
using Lockstep.Logic;
using Lockstep.Math;
using UnityEngine;
using Debug = Lockstep.Logging.Debug;

namespace LockstepTutorial {
public class Enemy : BaseEntity {
public IEnemyView eventHandler;
public BaseEntity target;
public class Enemy : BaseActor {
public BaseActor target;

public LFloat stopDistSqr = 1 * 1;
public LFloat atkInterval = 1;
private LFloat atkTimer;
public int damage = 10;

public Enemy(){
moveSpd = 2;
turnSpd = 150;
}

public override void DoUpdate(LFloat deltaTime){
//find target
var allPlayer = GameManager.allPlayers;
var minDist = LFloat.MaxValue;
BaseActor minTarget = null;
foreach (var player in allPlayer) {
if(player.isDead) continue;
var dist = (player.transform.pos - transform.pos).sqrMagnitude;
if (dist < minDist) {
minTarget = player;
minDist = dist;
}
}

target = minTarget;
if (minTarget == null)
return;
if (minDist > stopDistSqr) {
// turn to target
var targetPos = minTarget.transform.pos;
var currentPos = transform.pos;
var diff = targetPos - currentPos;
var dir = diff.normalized;
var turnVal = turnSpd * deltaTime;
var targetDeg = CTransform2D.TurnToward(targetPos, currentPos, transform.deg, turnVal,
out var isFinishedTurn);
transform.deg = targetDeg;
//move to target
var distToTarget = (targetPos - currentPos).magnitude;
var movingStep = moveSpd * deltaTime;
if (movingStep > distToTarget) {
movingStep = distToTarget;
}

var toTarget = (targetPos - currentPos).normalized;
transform.pos = transform.pos + toTarget * movingStep;
}
else {
//atk target
atkTimer -= deltaTime;
if (atkTimer <= 0) {
atkTimer = atkInterval;
//Atk
target.TakeDamage(damage, target.transform.Pos3);
}
}
}
protected override void OnDead(){
EnemyManager.Instance.RemoveEnemy(this);
}
}
}
3 changes: 1 addition & 2 deletions Unity/Assets/Scripts/Logic/Entity/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using UnityEngine.SceneManagement;

namespace LockstepTutorial {
public partial class Player : BaseEntity {
public IPlayerView eventHandler;
public partial class Player : BaseActor {
public PlayerInput InputAgent = new PlayerInput();
public CMover CMover = new CMover();
public int localId;
Expand Down
9 changes: 9 additions & 0 deletions Unity/Assets/Scripts/Logic/Interfaces/IActorView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Lockstep.Logic;
using Lockstep.Math;

namespace LockstepTutorial {
public interface IActorView : IView {
void OnTakeDamage(int amount, LVector3 hitPoint);
void OnDead();
}
}
3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/Logic/Interfaces/IActorView.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Unity/Assets/Scripts/Logic/Interfaces/IEnemyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
using Lockstep.Math;

namespace LockstepTutorial {
public interface IEnemyView : IView {
public interface IEnemyView : IActorView {
}
}
5 changes: 1 addition & 4 deletions Unity/Assets/Scripts/Logic/Interfaces/IPlayerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using Lockstep.Math;

namespace LockstepTutorial {
public interface IPlayerView :IView {
void TakeDamage(int amount, LVector3 hitPoint);
void OnDead();
void Animating(bool isIdle);
public interface IPlayerView : IActorView {
}
}
7 changes: 4 additions & 3 deletions Unity/Assets/Scripts/Logic/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private void Awake(){
Screen.SetResolution(1024, 768, false);
gameObject.AddComponent<PingMono>();
gameObject.AddComponent<InputMono>();


Lockstep.Logging.Logger.OnMessage += UnityLogHandler.OnLog;
_Awake();
}

Expand Down Expand Up @@ -315,12 +316,12 @@ public int GetHash(){
int hash = 1;
int idx = 0;
foreach (var entity in allPlayers) {
hash += entity.currentHealth.GetHash() * PrimerLUT.GetPrimer(idx++);
hash += entity.curHealth.GetHash() * PrimerLUT.GetPrimer(idx++);
hash += entity.transform.GetHash() * PrimerLUT.GetPrimer(idx++);
}

foreach (var entity in EnemyManager.Instance.allEnemy) {
hash += entity.currentHealth.GetHash() * PrimerLUT.GetPrimer(idx++);
hash += entity.curHealth.GetHash() * PrimerLUT.GetPrimer(idx++);
hash += entity.transform.GetHash() * PrimerLUT.GetPrimer(idx++);
}

Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Scripts/Logic/Service/UnityEntityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static GameObject CreateEntity(BaseEntity entity, int prefabId, LVector3
foreach (var view in views) {
view.BindEntity(entity);
}

entity.PrefabId = prefabId;
entity.DoAwake();
entity.DoStart();
Expand Down
23 changes: 23 additions & 0 deletions Unity/Assets/Scripts/Logic/Util/UnityLogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

using Lockstep.Logging;

namespace Lockstep.Game {
public class UnityLogHandler {
public static void OnLog(object sender, LogEventArgs args){
switch (args.LogSeverity) {
case LogSeverity.Info:
UnityEngine.Debug.Log(args.Message);
break;
case LogSeverity.Warn:
UnityEngine.Debug.LogWarning(args.Message);
break;
case LogSeverity.Error:
UnityEngine.Debug.LogError(args.Message);
break;
case LogSeverity.Exception:
UnityEngine.Debug.LogError(args.Message);
break;
}
}
}
}
11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Logic/Util/UnityLogHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions Unity/Assets/Scripts/View/EnemyView.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/View/EntityView.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Unity/Assets/Scripts/View/EntityView/BaseActorView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Lockstep.Logic;
using Lockstep.Math;
using UnityEngine;

namespace LockstepTutorial {
public abstract class BaseActorView : MonoBehaviour, IActorView {
public UIFloatBar uiFloatBar;
public BaseActor baseActor;
protected bool isDead => baseActor?.isDead ?? true;

public virtual void BindEntity(BaseEntity entity){
baseActor = entity as BaseActor;
baseActor.actorView = this;
uiFloatBar = FloatBarManager.CreateFloatBar(transform, baseActor.curHealth, baseActor.maxHealth);
transform.position = baseActor.transform.Pos3.ToVector3();
}

public virtual void OnTakeDamage(int amount, LVector3 hitPoint){
uiFloatBar.UpdateHp(baseActor.curHealth, baseActor.maxHealth);
FloatTextManager.CreateFloatText(hitPoint.ToVector3(), -amount);
}

public virtual void OnDead(){
if (uiFloatBar != null) FloatBarManager.DestroyText(uiFloatBar);
GameObject.Destroy(gameObject);
}

private void Update(){
var pos = baseActor.transform.Pos3.ToVector3();
transform.position = Vector3.Lerp(transform.position, pos, 0.3f);
var deg = baseActor.transform.deg.ToFloat();
//deg = Mathf.Lerp(transform.rotation.eulerAngles.y, deg, 0.3f);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, deg, 0), 0.3f);
}
}
}
3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/View/EntityView/BaseActorView.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Unity/Assets/Scripts/View/EntityView/EnemyView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections;
using Lockstep.Logic;
using Lockstep.Math;
using UnityEngine;

namespace LockstepTutorial {
public class EnemyView : BaseActorView, IEnemyView {
}
}
File renamed without changes.
Loading

0 comments on commit e40520c

Please sign in to comment.