|
@@ -1,7 +1,12 @@
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Security.Policy;
|
|
|
using System.Xml;
|
|
|
using Sfs2X;
|
|
|
+using Sfs2X.Core;
|
|
|
+using Sfs2X.Entities.Data;
|
|
|
+using Sfs2X.Requests;
|
|
|
+using Sfs2X.Util;
|
|
|
using UnityEngine;
|
|
|
|
|
|
public class Robot
|
|
@@ -25,10 +30,6 @@ public class Robot
|
|
|
private static string RootNodeName = "data";
|
|
|
private static string DataNodeName = "item";
|
|
|
|
|
|
-
|
|
|
- private float LifetimeTimer;
|
|
|
- private float MoveTimer;
|
|
|
-
|
|
|
private SmartFox SmartFox;
|
|
|
|
|
|
#endregion
|
|
@@ -63,28 +64,193 @@ public class Robot
|
|
|
MinLifetime = ints[0];
|
|
|
MaxLifetime = ints[1];
|
|
|
|
|
|
- Debug.Log($"RobotConfig {MinRobotCreateTime}");
|
|
|
- Debug.Log($"RobotConfig {MaxRobotCreateTime}");
|
|
|
- Debug.Log($"RobotConfig {MaxRobotAmtFml}");
|
|
|
- Debug.Log($"RobotConfig {DigitChestRate}");
|
|
|
- Debug.Log($"RobotConfig {ColorChestRate}");
|
|
|
- Debug.Log($"RobotConfig {MinMoveTime}");
|
|
|
- Debug.Log($"RobotConfig {MaxMoveTime}");
|
|
|
- Debug.Log($"RobotConfig {CreateChestRate}");
|
|
|
- Debug.Log($"RobotConfig {MinChestValue}");
|
|
|
- Debug.Log($"RobotConfig {MaxChestValue}");
|
|
|
- Debug.Log($"RobotConfig {MinLifetime}");
|
|
|
- Debug.Log($"RobotConfig {MaxLifetime}");
|
|
|
+ //Debug.Log($"RobotConfig {MinRobotCreateTime}");
|
|
|
+ //Debug.Log($"RobotConfig {MaxRobotCreateTime}");
|
|
|
+ //Debug.Log($"RobotConfig {MaxRobotAmtFml}");
|
|
|
+ //Debug.Log($"RobotConfig {DigitChestRate}");
|
|
|
+ //Debug.Log($"RobotConfig {ColorChestRate}");
|
|
|
+ //Debug.Log($"RobotConfig {MinMoveTime}");
|
|
|
+ //Debug.Log($"RobotConfig {MaxMoveTime}");
|
|
|
+ //Debug.Log($"RobotConfig {CreateChestRate}");
|
|
|
+ //Debug.Log($"RobotConfig {MinChestValue}");
|
|
|
+ //Debug.Log($"RobotConfig {MaxChestValue}");
|
|
|
+ //Debug.Log($"RobotConfig {MinLifetime}");
|
|
|
+ //Debug.Log($"RobotConfig {MaxLifetime}");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Connect(int sfsRoomID, int maxRobot)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot connect");
|
|
|
+ SmartFox = new SmartFox();
|
|
|
+ SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnectReturn);
|
|
|
+ SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnActivateError);
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGIN, evt => { Mark(sfsRoomID, maxRobot); });
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnActivateError);
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGOUT, OnActivateError);
|
|
|
+ SmartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
|
|
|
+
|
|
|
+ ConfigData configData = new ConfigData
|
|
|
+ {
|
|
|
+ Host = BaseConnector.Host,
|
|
|
+ Port = BaseConnector.TcpPort,
|
|
|
+ Zone = BaseConnector.Zone,
|
|
|
+ };
|
|
|
+ SmartFox.Connect(configData);
|
|
|
}
|
|
|
|
|
|
+ private void Login()
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot login");
|
|
|
+ string userName = Random.Range(0f, 1000f).ToString();
|
|
|
+ SmartFox.Send(new LoginRequest(userName, ""));
|
|
|
+ }
|
|
|
|
|
|
- public void Start()
|
|
|
+ private void Mark(int sfsRoomID, int maxRobot)
|
|
|
{
|
|
|
+ Debug.LogWarning("Robot mark");
|
|
|
+ SFSObject arg = new SFSObject();
|
|
|
+ arg.PutInt(Label.CommandID, PlazaRoomReq.MarkAsRobot.GetHashCode());
|
|
|
+ arg.PutInt(Label.RoomID, sfsRoomID);
|
|
|
+ arg.PutInt(Label.Data, maxRobot);
|
|
|
+ SendRequest(arg);
|
|
|
+ }
|
|
|
|
|
|
+ private void SendRequest(SFSObject arg)
|
|
|
+ {
|
|
|
+ arg = GardenSmartFox.WrapIntoArray(new List<SFSObject> { arg });
|
|
|
+ SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), arg));
|
|
|
}
|
|
|
|
|
|
- public void Update()
|
|
|
+
|
|
|
+ private void OnConnectReturn(BaseEvent evt)
|
|
|
{
|
|
|
+ if ((bool)evt.Params["success"])
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot connect succeed");
|
|
|
+ Login();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot connect failed");
|
|
|
+ OnActivateError(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private void OnExtensionResponse(BaseEvent evt)
|
|
|
+ {
|
|
|
+ string cmd = evt.Params["cmd"].ToString();
|
|
|
+
|
|
|
+ if (cmd == PlazaRoomRep.JoinRoomSucceed.GetHashString())
|
|
|
+ {
|
|
|
+ Activate();
|
|
|
+ }
|
|
|
+ else if (cmd == PlazaRoomRep.MarkRobotError.GetHashString())
|
|
|
+ {
|
|
|
+ OnActivateError(evt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public bool Update()
|
|
|
+ {
|
|
|
+ if (SmartFox != null)
|
|
|
+ {
|
|
|
+ SmartFox.ProcessEvents();
|
|
|
+ }
|
|
|
+
|
|
|
+ return Behaviour();
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool Activated;
|
|
|
+ private void Activate()
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot Activate");
|
|
|
+ Activated = true;
|
|
|
+ MoveTime = Random.Range(MinMoveTime, MaxMoveTime);
|
|
|
+ LifetimeTime = Random.Range(MinLifetime, MaxLifetime);
|
|
|
+ CreateChestTime = Random.Range(0, LifetimeTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool Behaviour()
|
|
|
+ {
|
|
|
+ if (!Activated)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (LifetimeBehaviour())
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ MoveBehaviour();
|
|
|
+ CreateChestBehaviour();
|
|
|
+ GetChestAwardBehaviour();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private float MoveTime;
|
|
|
+ private float MoveTimer;
|
|
|
+ private void MoveBehaviour()
|
|
|
+ {
|
|
|
+ MoveTimer += Time.deltaTime;
|
|
|
+ if (MoveTimer >= MoveTime)
|
|
|
+ {
|
|
|
+ MoveTimer = 0;
|
|
|
+ Debug.LogWarning("Robot move");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private float LifetimeTime;
|
|
|
+ private float LifetimeTimer;
|
|
|
+ private bool LifetimeBehaviour()
|
|
|
+ {
|
|
|
+ LifetimeTimer += Time.deltaTime;
|
|
|
+ if (LifetimeTimer >= LifetimeTime)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot lifetime over");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool CreateChestLock;
|
|
|
+ private float CreateChestTime;
|
|
|
+ private float CreateChestTimer;
|
|
|
+ private void CreateChestBehaviour()
|
|
|
+ {
|
|
|
+ if (CreateChestLock)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ CreateChestTimer += Time.deltaTime;
|
|
|
+ if (CreateChestTimer >= CreateChestTime)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot create chest");
|
|
|
+ CreateChestLock = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void GetChestAwardBehaviour()
|
|
|
+ {
|
|
|
+ //Debug.LogWarning("robot get a chest");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void Deactivate()
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot disconnect");
|
|
|
+ SmartFox.Disconnect();
|
|
|
+ Activated = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnActivateError(BaseEvent evt)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("Robot activate error");
|
|
|
+ RobotManager.Robots.Remove(this);
|
|
|
}
|
|
|
}
|