|
@@ -1,18 +1,26 @@
|
|
|
-using System.Collections;
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
using System.Security.Policy;
|
|
|
using System.Xml;
|
|
|
using Sfs2X;
|
|
|
using Sfs2X.Core;
|
|
|
+using Sfs2X.Entities;
|
|
|
using Sfs2X.Entities.Data;
|
|
|
using Sfs2X.Requests;
|
|
|
using Sfs2X.Util;
|
|
|
using UnityEngine;
|
|
|
+using Random = UnityEngine.Random;
|
|
|
|
|
|
public class Robot
|
|
|
{
|
|
|
#region Config
|
|
|
|
|
|
+ private static Vector2 LeftDownBorder; //todo
|
|
|
+ private static Vector2 RightTop; //todo
|
|
|
+ private static Camera Camera; //todo
|
|
|
+
|
|
|
private static bool Initialized;
|
|
|
private static int MinChestValue;
|
|
|
private static int MaxChestValue;
|
|
@@ -30,8 +38,15 @@ public class Robot
|
|
|
private static string RootNodeName = "data";
|
|
|
private static string DataNodeName = "item";
|
|
|
|
|
|
+ private string Nickname;
|
|
|
+ private Vector3 Position;
|
|
|
+ private PlayerDirection Direction;
|
|
|
+ private User User;
|
|
|
private SmartFox SmartFox;
|
|
|
-
|
|
|
+
|
|
|
+ private List<int> DressdataIDs = new List<int>();
|
|
|
+ private List<string> DressdataNames = new List<string>();
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
public static void Initialize()
|
|
@@ -42,6 +57,10 @@ public class Robot
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ LeftDownBorder = ManaReso.Get("PlayerLeftDownBorder").position;
|
|
|
+ RightTop = ManaReso.Get("PlayerRightTopBorder").position;
|
|
|
+ Camera = ManaReso.Get<Camera>("MainCamera");
|
|
|
+
|
|
|
XmlDocument document = ManaData.GetXmlDocument(ConfigName);
|
|
|
XmlNode dataNode = document.SelectSingleNode(RootNodeName).SelectSingleNode(DataNodeName);
|
|
|
XmlAttributeCollection attributes = dataNode.Attributes;
|
|
@@ -78,15 +97,22 @@ public class Robot
|
|
|
//Debug.Log($"RobotConfig {MaxLifetime}");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public Robot(Vector3 position, PlayerDirection direction)
|
|
|
+ {
|
|
|
+ Position = position;
|
|
|
+ Direction = direction;
|
|
|
+ }
|
|
|
+
|
|
|
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.CONNECTION_LOST, (evt) => Deactivate(false));
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGIN, evt => { Mark(evt, sfsRoomID, maxRobot); });
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGIN_ERROR, (evt) => Deactivate(false));
|
|
|
+ SmartFox.AddEventListener(SFSEvent.LOGOUT, (evt) => Deactivate(false));
|
|
|
SmartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
|
|
|
|
|
|
ConfigData configData = new ConfigData
|
|
@@ -105,9 +131,10 @@ public class Robot
|
|
|
SmartFox.Send(new LoginRequest(userName, ""));
|
|
|
}
|
|
|
|
|
|
- private void Mark(int sfsRoomID, int maxRobot)
|
|
|
+ private void Mark(BaseEvent evt, int sfsRoomID, int maxRobot)
|
|
|
{
|
|
|
Debug.LogWarning("Robot mark");
|
|
|
+ User = (User) evt.Params["user"];
|
|
|
SFSObject arg = new SFSObject();
|
|
|
arg.PutInt(Label.CommandID, PlazaRoomReq.MarkAsRobot.GetHashCode());
|
|
|
arg.PutInt(Label.RoomID, sfsRoomID);
|
|
@@ -117,14 +144,19 @@ public class Robot
|
|
|
|
|
|
private void SendRequest(SFSObject arg)
|
|
|
{
|
|
|
- arg = GardenSmartFox.WrapIntoArray(new List<SFSObject> { arg });
|
|
|
+ arg = GardenSmartFox.WrapIntoArray(new List<SFSObject> {arg});
|
|
|
SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), arg));
|
|
|
}
|
|
|
|
|
|
+ private void SendInstantiateRequest(int receiver)
|
|
|
+ {
|
|
|
+ SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.SendInstantiateRequset(User.Id, DressdataIDs.ToArray(), Position, Direction, Nickname, receiver);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
private void OnConnectReturn(BaseEvent evt)
|
|
|
{
|
|
|
- if ((bool)evt.Params["success"])
|
|
|
+ if ((bool) evt.Params["success"])
|
|
|
{
|
|
|
Debug.LogWarning("Robot connect succeed");
|
|
|
Login();
|
|
@@ -132,7 +164,7 @@ public class Robot
|
|
|
else
|
|
|
{
|
|
|
Debug.LogWarning("Robot connect failed");
|
|
|
- OnActivateError(null);
|
|
|
+ Deactivate(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -142,11 +174,26 @@ public class Robot
|
|
|
|
|
|
if (cmd == PlazaRoomRep.JoinRoomSucceed.GetHashString())
|
|
|
{
|
|
|
- Activate();
|
|
|
+ Activate(evt);
|
|
|
}
|
|
|
else if (cmd == PlazaRoomRep.MarkRobotError.GetHashString())
|
|
|
{
|
|
|
- OnActivateError(evt);
|
|
|
+ Deactivate(false);
|
|
|
+ }
|
|
|
+ else if (cmd == PlazaRoomRep.OtherEnterRoom.GetHashString()) //todo
|
|
|
+ {
|
|
|
+ OnOtherEnterRoom(evt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnOtherEnterRoom(BaseEvent evt)
|
|
|
+ {
|
|
|
+ ISFSObject arg = (ISFSObject) evt.Params["params"];
|
|
|
+ int userID = arg.GetInt(Label.UserID);
|
|
|
+
|
|
|
+ if (userID != User.Id)
|
|
|
+ {
|
|
|
+ SendInstantiateRequest(userID);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -162,13 +209,32 @@ public class Robot
|
|
|
}
|
|
|
|
|
|
private bool Activated;
|
|
|
- private void Activate()
|
|
|
+ private void Activate(BaseEvent evt)
|
|
|
{
|
|
|
+ if (!SFSManager.GardenSmartFox.PlazaRoomManager.InPlazaRoom)
|
|
|
+ {
|
|
|
+ Deactivate(false);
|
|
|
+ }
|
|
|
+
|
|
|
Debug.LogWarning("Robot Activate");
|
|
|
Activated = true;
|
|
|
MoveTime = Random.Range(MinMoveTime, MaxMoveTime);
|
|
|
LifetimeTime = Random.Range(MinLifetime, MaxLifetime);
|
|
|
CreateChestTime = Random.Range(0, LifetimeTime);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ISFSObject arg = evt.Params["params"] as ISFSObject;
|
|
|
+ Nickname = arg.GetUtfString(Label.Data);
|
|
|
+ DressdataNames = arg.GetUtfStringArray(Label.DataArray).ToList();
|
|
|
+ DressdataIDs = ManaData.GetDressDataIDs(DressdataNames);
|
|
|
+ SendInstantiateRequest(-1);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Debug.LogWarning(e);
|
|
|
+ Deactivate(false);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private bool Behaviour()
|
|
@@ -178,14 +244,14 @@ public class Robot
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (LifetimeBehaviour())
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ //if (LifetimeBehaviour())
|
|
|
+ //{
|
|
|
+ // return true;
|
|
|
+ //}
|
|
|
|
|
|
- MoveBehaviour();
|
|
|
- CreateChestBehaviour();
|
|
|
- GetChestAwardBehaviour();
|
|
|
+ //MoveBehaviour();
|
|
|
+ //CreateChestBehaviour();
|
|
|
+ //GetChestAwardBehaviour();
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -198,9 +264,28 @@ public class Robot
|
|
|
{
|
|
|
MoveTimer = 0;
|
|
|
Debug.LogWarning("Robot move");
|
|
|
+ Raycast();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void Raycast() //todo
|
|
|
+ {
|
|
|
+ Vector3 origin = new Vector3(Random.Range(LeftDownBorder.x, RightTop.x), Random.Range(LeftDownBorder.y, RightTop.y), Camera.transform.position.z);
|
|
|
+ Ray ray = new Ray(origin, Vector3.forward);
|
|
|
+ RaycastHit hitInfo;
|
|
|
+ if (Physics.Raycast(ray, out hitInfo))
|
|
|
+ {
|
|
|
+ Vector3 destination = PlazaRoom.HitPositionToDestination(hitInfo.point);
|
|
|
+ SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.SendSynchronizeDestination(User.Id, destination);
|
|
|
+ Position = destination;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning("射线检测失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private float LifetimeTime;
|
|
|
private float LifetimeTimer;
|
|
|
private bool LifetimeBehaviour()
|
|
@@ -220,7 +305,7 @@ public class Robot
|
|
|
private bool CreateChestLock;
|
|
|
private float CreateChestTime;
|
|
|
private float CreateChestTimer;
|
|
|
- private void CreateChestBehaviour()
|
|
|
+ private void CreateChestBehaviour() //todo
|
|
|
{
|
|
|
if (CreateChestLock)
|
|
|
{
|
|
@@ -230,27 +315,133 @@ public class Robot
|
|
|
CreateChestTimer += Time.deltaTime;
|
|
|
if (CreateChestTimer >= CreateChestTime)
|
|
|
{
|
|
|
- Debug.LogWarning("Robot create chest");
|
|
|
CreateChestLock = true;
|
|
|
+ if (Random.Range(0f, 1f) >= CreateChestRate)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("create chest ran out of luck");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.LogWarning("Robot create a chest");
|
|
|
+ RoomData roomData = SFSManager.GardenSmartFox.PlazaRoomManager.CurrentRoomData;
|
|
|
+ int round = Random.Range(1, roomData.MaxPlayer + 1);
|
|
|
+ int value = Random.Range(MinChestValue, MaxChestValue);
|
|
|
+ Vector3 position = ChestMge.PlayerPosToChestPos(Position);
|
|
|
+ ChestType type = (ChestType) Random.Range(1, 4);
|
|
|
+ SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.CreateChest(round, value, type, roomData.ID, -1, position);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void GetChestAwardBehaviour()
|
|
|
+ private float GetChestTime = 3;
|
|
|
+ private float GetChestTimer;
|
|
|
+ private float MaxChestDistance = 8; //todo
|
|
|
+ private List<long> OperatedChestIDs = new List<long>();
|
|
|
+ private void GetChestAwardBehaviour() //todo
|
|
|
{
|
|
|
+ GetChestTimer += Time.deltaTime;
|
|
|
+ if (GetChestTimer >= GetChestTime)
|
|
|
+ {
|
|
|
+ GetChestTimer = 0;
|
|
|
+ Debug.LogWarning("Robot try get some chests");
|
|
|
+ List<PlazaRoomChest> chests = DetectChest();
|
|
|
+ Debug.LogWarning($"{chests.Count} chest/chests is available");
|
|
|
+ for (int i = 0; i < chests.Count; i++)
|
|
|
+ {
|
|
|
+ TryGetChestAward(chests[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
//Debug.LogWarning("robot get a chest");
|
|
|
}
|
|
|
|
|
|
+ private List<PlazaRoomChest> DetectChest()
|
|
|
+ {
|
|
|
+ List<PlazaRoomChest> chests = new List<PlazaRoomChest>();
|
|
|
+
|
|
|
+ for (int i = 0; i < ChestMge.PlazaRoomChests.Count; i++)
|
|
|
+ {
|
|
|
+ if (Mathf.Abs(ChestMge.PlazaRoomChests[i].transform.position.x - Position.x) <= MaxChestDistance)
|
|
|
+ {
|
|
|
+ if (OperatedChestIDs.Contains(ChestMge.PlazaRoomChests[i].ChestData.ID))
|
|
|
+ {
|
|
|
+ Debug.LogWarning("this chest has been operated");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ chests.Add(ChestMge.PlazaRoomChests[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning("this chest is too far");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return chests;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void TryGetChestAward(PlazaRoomChest chest)
|
|
|
+ {
|
|
|
+ float rate = Random.Range(0f, 1f);
|
|
|
+ bool get = false;
|
|
|
+ if (chest.ChestData.ChestType == ChestType.GuessColor)
|
|
|
+ {
|
|
|
+ if (rate < ColorChestRate)
|
|
|
+ {
|
|
|
+ get = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ get = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (chest.ChestData.ChestType == ChestType.GuessNumber)
|
|
|
+ {
|
|
|
+ if (rate < DigitChestRate)
|
|
|
+ {
|
|
|
+ get = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ get = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ get = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ OperatedChestIDs.Add(chest.ChestData.ID);
|
|
|
+ if (get)
|
|
|
+ {
|
|
|
+ SFSManager.GardenSmartFox.EventManager.PlazaRoomEvent.GetChestAward(chest.ChestData.ID);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning("get chest ran out of luck");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- public void Deactivate()
|
|
|
+ private static float MinDelayTime = 3f;
|
|
|
+ private static float MaxDelayTime = 10f;
|
|
|
+ public void Deactivate(bool delay)
|
|
|
{
|
|
|
Debug.LogWarning("Robot disconnect");
|
|
|
- SmartFox.Disconnect();
|
|
|
Activated = false;
|
|
|
+
|
|
|
+ if (delay)
|
|
|
+ {
|
|
|
+ DelayCall.Call(Random.Range(MinDelayTime, MaxDelayTime), Remove);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Remove();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void OnActivateError(BaseEvent evt)
|
|
|
+ private void Remove()
|
|
|
{
|
|
|
- Debug.LogWarning("Robot activate error");
|
|
|
RobotManager.Robots.Remove(this);
|
|
|
+ SmartFox.Disconnect();
|
|
|
}
|
|
|
}
|