internal void AddUnit(MsgAddUnit_S2C config) { GameObject go = new GameObject("unit_" + config.id.ToString()); go.transform.position = new Vector3(config.posx, 0, config.posy); if (config.isController == 1) { FollowCamera camera = Camera.main.GetComponent <FollowCamera>(); if (camera == null) { camera = Camera.main.gameObject.AddComponent <FollowCamera>(); } camera.m_target = go.transform; go.AddComponent <Controller>(); } Unit unit = go.AddComponent <Unit>(); m_units.Add(config.id, unit); unit.ID = config.id; unit.camp = config.camp; unit.isController = config.isController == 1; unit.transform.position = new Vector3(config.posx, 0, config.posy); CapsuleCollider collider = unit.gameObject.AddComponent <CapsuleCollider>(); collider.radius = 3; collider.height = 2; collider.center = new Vector3(0, 1, 0); unit.fsm.blackboard.unit = unit; unit.fsm.AttachFactory(m_unitFsmStateFactory); unit.fsm.Transition((int)UnitFSM.State.ID.Idle); unit.ChangeModel(config.look); }
public Unit AddUnit(int config_id, int camp, Position position, Player controller) { UnitConfig config = database.GetUnitConfig(config_id); if (config == null) { m_context.Log(string.Format("AddUnit({0}) failed", config_id)); return(null); } Unit unit = new Unit(); unit.id = ++m_increase_id; unit.camp = camp; unit.game = this; unit.position = position; unit.SetAttribute(Attribute.Hp, config.hp); unit.SetAttribute(Attribute.Mp, config.hp); unit.SetAttribute(Attribute.Atk, config.atk); unit.SetAttribute(Attribute.Def, config.def); unit.SetAttribute(Attribute.MAtk, config.matk); unit.SetAttribute(Attribute.MDef, config.mdef); unit.SetAttribute(Attribute.Spd, config.spd); unit.fsm.blackboard.unit = unit; unit.fsm.AttachFactory(m_unitFsmStateFactory); unit.fsm.Transition((int)UnitFSM.State.ID.Idle); SetSkillToSlot(unit, config.skill0, 0); SetSkillToSlot(unit, config.skill1, 1); SetSkillToSlot(unit, config.skill2, 2); SetSkillToSlot(unit, config.skill3, 3); if (controller == null) { unit.ai = new AIPawn(unit); } m_units.Add(unit); foreach (var pair in m_players) { Player player = pair.Value; MsgAddUnit_S2C msg = new MsgAddUnit_S2C(); msg.id = unit.id; msg.camp = unit.camp; msg.look = config.look; msg.isController = (int)(player == controller ? 1 : 0); msg.posx = unit.position.x; msg.posy = unit.position.y; player.Send(msg); } m_context.Log(string.Format("AddUnit id={0}, camp={4}, look={1}, pos=({2},{3})", unit.id, config.look, unit.position.x, unit.position.y, unit.camp)); return(unit); }
public void ProcessMsg(ushort _type) { MsgType type = (MsgType)_type; switch (type) { case MsgType.Login: { SceneManager.LoadSceneAsync("Lobby"); m_state = State.Login; } break; case MsgType.Matching: { m_state = State.Matching; } break; case MsgType.CancelMatching: { m_state = State.Login; } break; case MsgType.NewGame: { Debug.Log("NewGame"); m_state = State.InGame; Game.Configs configs = new Game.Configs(); configs.map = NetSession.In.ReadInt32(); int playerCount = NetSession.In.ReadInt32(); for (int i = 0; i < playerCount; i++) { string name = System.Text.Encoding.Default.GetString(NetSession.In.ReadBytes(16)); name = name.Substring(0, name.IndexOf(char.MinValue)); int id = NetSession.In.ReadInt32(); Game.Configs.Player player = new Game.Configs.Player(); player.id = id; configs.players.Add(player); Debug.LogFormat("Player {0}({1}) incoming", name, id); } Core.NewGame(configs); } break; case MsgType.StartLoad: { Core.StartLoad(); } break; case MsgType.EndGame: { Debug.Log("EndGame"); } break; case MsgType.AddUnit: { MsgAddUnit_S2C msg = NetSession.In.Read <MsgAddUnit_S2C>(); Core.Game.AddUnit(msg); } break; case MsgType.AddBullet: { MsgAddBullet_S2C msg = NetSession.In.Read <MsgAddBullet_S2C>(); Core.Game.AddBullet(msg); } break; case MsgType.MoveTo: { MsgMoveTo_S2C msg = NetSession.In.Read <MsgMoveTo_S2C>(); Unit unit = Core.Game.FindUnit(msg.id); if (unit) { unit.MoveTo(msg.posx, msg.posy, msg.spd * 0.01f); } } break; case MsgType.DoAction: { MsgDoAction_S2C msg = NetSession.In.Read <MsgDoAction_S2C>(); Unit unit = Core.Game.FindUnit(msg.id); if (unit) { unit.DoAction(msg.posx, msg.posy, msg.duration, msg.action, msg.effect, msg.dir); } } break; default: { Debug.LogFormat("error msg type {0}.", type.ToString()); } break; } }