Example #1
0
        public static void BuildAndInstantiate(MonsterFactoryOpts opts)
        {
            var monsterObj = GameObject.Find(opts.MonsterUid);

            if (monsterObj == null)
            {
                monsterObj = new GameObject(opts.MonsterUid);
                monsterObj.transform.localScale = new Vector3(100, 100);

                FactoryMethods.AddCollider(monsterObj);
                var spriteRenderer = monsterObj.AddComponent <SpriteRenderer>();
                var spriteSheet    = monsterObj.AddComponent <SpriteSheet>();
                spriteRenderer.sortingOrder = 2;
                var bodySprite = AssetHandler.LoadedAssets[DefaultAssets.SPR_MONTERS_1];
                var spriteRow  = bodySprite.SliceRow(opts.SpriteIndex).ToArray();
                spriteSheet.SetSheet(spriteRow, rowSize: 2);
                spriteRenderer.sprite = spriteSheet.WalkSouth[1];
                var movingBehaviour = monsterObj.AddComponent <MovingEntityBehaviour>();
                movingBehaviour.SpriteSheets.Add(spriteSheet);
                movingBehaviour.MoveSpeed   = opts.MoveSpeed;
                movingBehaviour.MapPosition = opts.Position;
                var monsterEntityWrapper = new MonsterWrapper()
                {
                    MonsterObj = monsterObj
                };
                movingBehaviour.EntityWrapper = monsterEntityWrapper;
                monsterObj.transform.position = new Vector2(opts.Position.X * 16, -opts.Position.Y * 16);
                UnityClient.Map.EntityPositions.AddEntity(monsterEntityWrapper, opts.Position);
            }
        }
Example #2
0
        public static void BuildAndInstantiate(MonsterFactoryOpts opts)
        {
            var monsterObj = GameObject.Find(opts.Packet.MonsterUid);

            if (monsterObj == null)
            {
                monsterObj = new GameObject(opts.Packet.MonsterUid);
                monsterObj.transform.localScale = new Vector3(100, 100);

                var spriteObj = new GameObject("Sprites_" + opts.Packet.MonsterUid);
                spriteObj.transform.localScale = new Vector3(100, 100);

                var spriteRenderer = spriteObj.AddComponent <SpriteRenderer>();
                var spriteSheet    = spriteObj.AddComponent <SpriteSheet>();
                spriteRenderer.sortingOrder = 2;
                var bodySprite = AssetHandler.LoadedAssets[DefaultAssets.SPR_MONTERS_1];
                var spriteRow  = bodySprite.SliceRow(opts.Packet.SpriteIndex).ToArray();
                spriteSheet.SetSheet(spriteRow, rowSize: 2);
                spriteRenderer.sprite      = spriteSheet.WalkSouth[1];
                spriteObj.transform.parent = monsterObj.transform;

                // Moving Entity
                var livingEntityBhv = monsterObj.AddComponent <LivingEntityBehaviour>();
                livingEntityBhv.SpriteSheets.Add(spriteSheet);
                var monsterEntityWrapper = new MonsterWrapper()
                {
                    MonsterObj = monsterObj,
                    EntityType = EntityType.MONSTER,
                    MoveSpeed  = opts.Packet.MoveSpeed,
                    UID        = opts.Packet.MonsterUid,
                    Position   = opts.Position,
                    Atk        = opts.Packet.Atk,
                    Def        = opts.Packet.Def,
                    HP         = opts.Packet.HP,
                    MAXHP      = opts.Packet.MAXHP
                };

                monsterObj.transform.parent = UnityExtensions.GetEntitiesContainer().transform;

                livingEntityBhv.Entity        = monsterEntityWrapper;
                monsterObj.transform.position = opts.Position.ToUnityPosition();
                UnityClient.Map.UpdateEntityPosition(monsterEntityWrapper, null, opts.Position);

                FactoryMethods.AddCollider(monsterObj);
                FactoryMethods.AddHealthBar(monsterObj);
            }
        }
Example #3
0
        public static void BuildAndInstantiate(PlayerFactoryOptions opts)
        {
            var playerObj = GameObject.Find(opts.UserId);

            if (playerObj == null)
            {
                // Player Obj
                playerObj = new GameObject(opts.UserId);
                playerObj.transform.localScale = new Vector3(100, 100);
                playerObj.tag = "Player";
                FactoryMethods.AddCollider(playerObj);
                if (opts.IsMainPlayer)
                {
                    playerObj.AddComponent <PlayerBehaviour>();
                    UnityClient.Player.Speed        = opts.Speed;
                    UnityClient.Player.PlayerObject = playerObj;
                    UnityClient.Player.Position     = new Position(opts.tileX, opts.tileY);
                }

                // Body
                var bodyObj = new GameObject("body");
                bodyObj.transform.localScale = new Vector3(100, 100);
                var spriteRenderer = bodyObj.AddComponent <SpriteRenderer>();
                var spriteSheet    = bodyObj.AddComponent <SpriteSheet>();
                spriteRenderer.sortingOrder = 2;
                var bodySprite = AssetHandler.LoadedAssets[DefaultAssets.SPR_BODIES];
                var spriteRow  = bodySprite.SliceRow(opts.BodySpriteIndex).ToArray();
                spriteSheet.SetSheet(spriteRow);
                spriteRenderer.sprite    = spriteSheet.WalkSouth[1];
                bodyObj.transform.parent = playerObj.transform;

                // LEGS
                var legsObj = new GameObject("legs");
                legsObj.transform.localScale = new Vector3(100, 100);
                var legsSpriteRenderer = legsObj.AddComponent <SpriteRenderer>();
                var legsSpriteSheet    = legsObj.AddComponent <SpriteSheet>();
                legsSpriteRenderer.sortingOrder = 3;
                var legsSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_LEGS];
                var legsSpriteRow = legsSprite.SliceRow(opts.LegsSpriteIndex).ToArray();
                legsSpriteSheet.SetSheet(legsSpriteRow);
                legsSpriteRenderer.sprite = legsSpriteSheet.WalkSouth[1];
                legsObj.transform.parent  = playerObj.transform;

                // Chest
                var chestObj = new GameObject("chest");
                chestObj.transform.localScale = new Vector3(100, 100);
                var chestSpriteRenderer = chestObj.AddComponent <SpriteRenderer>();
                var chestSpriteSheet    = chestObj.AddComponent <SpriteSheet>();
                chestSpriteRenderer.sortingOrder = 4;
                var chestSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_CHESTS];
                var chestSpriteRow = chestSprite.SliceRow(opts.ChestSpriteIndex).ToArray();
                chestSpriteSheet.SetSheet(chestSpriteRow);
                chestSpriteRenderer.sprite = chestSpriteSheet.WalkSouth[1];
                chestObj.transform.parent  = playerObj.transform;

                // Head
                var headObj = new GameObject("head");
                headObj.transform.localScale = new Vector3(100, 100);
                var headSpriteRenderer = headObj.AddComponent <SpriteRenderer>();
                var headSpriteSheet    = headObj.AddComponent <SpriteSheet>();
                headSpriteRenderer.sortingOrder = 5;
                var headSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_HEADS];
                var headSpriteRow = headSprite.SliceRow(opts.HeadSpriteIndex).ToArray();
                headSpriteSheet.SetSheet(headSpriteRow);
                headSpriteRenderer.sprite = headSpriteSheet.WalkSouth[1];
                headObj.transform.parent  = playerObj.transform;

                // This gotta be in the end
                playerObj.transform.position = new Vector2(opts.tileX * 16, -opts.tileY * 16);

                if (!opts.IsMainPlayer)
                {
                    var movingEntity = playerObj.AddComponent <MovingEntityBehaviour>();
                    movingEntity.MoveSpeed   = opts.Speed;
                    movingEntity.MapPosition = new Position(opts.tileX, opts.tileY);
                    movingEntity.SpriteSheets.Add(spriteSheet);
                    movingEntity.SpriteSheets.Add(headSpriteSheet);
                    movingEntity.SpriteSheets.Add(legsSpriteSheet);
                    movingEntity.SpriteSheets.Add(chestSpriteSheet);
                    var playerWrapper = new PlayerWrapper()
                    {
                        PlayerObject = playerObj
                    };
                    movingEntity.EntityWrapper = playerWrapper;
                    UnityClient.Map.EntityPositions.AddEntity(movingEntity.EntityWrapper, new Position(opts.tileX, opts.tileY));
                }
            }
        }
Example #4
0
        public static void BuildAndInstantiate(PlayerFactoryOptions opts)
        {
            var playerObj = GameObject.Find(opts.UserId);

            if (playerObj == null)
            {
                // Player Obj
                playerObj = new GameObject(opts.UserId);
                playerObj.transform.localScale = new Vector3(100, 100);
                playerObj.tag = "Player";
                FactoryMethods.AddCollider(playerObj);

                // Body
                var bodyObj = new GameObject("body");
                bodyObj.transform.localScale = new Vector3(100, 100);
                var spriteRenderer = bodyObj.AddComponent <SpriteRenderer>();
                var spriteSheet    = bodyObj.AddComponent <SpriteSheet>();
                spriteRenderer.sortingOrder = 2;
                var bodySprite = AssetHandler.LoadedAssets[DefaultAssets.SPR_BODIES];
                var spriteRow  = bodySprite.SliceRow(opts.BodySpriteIndex).ToArray();
                spriteSheet.SetSheet(spriteRow);
                spriteRenderer.sprite    = spriteSheet.WalkSouth[1];
                bodyObj.transform.parent = playerObj.transform;

                // LEGS
                var legsObj = new GameObject("legs");
                legsObj.transform.localScale = new Vector3(100, 100);
                var legsSpriteRenderer = legsObj.AddComponent <SpriteRenderer>();
                var legsSpriteSheet    = legsObj.AddComponent <SpriteSheet>();
                legsSpriteRenderer.sortingOrder = 3;
                var legsSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_LEGS];
                var legsSpriteRow = legsSprite.SliceRow(opts.LegsSpriteIndex).ToArray();
                legsSpriteSheet.SetSheet(legsSpriteRow);
                legsSpriteRenderer.sprite = legsSpriteSheet.WalkSouth[1];
                legsObj.transform.parent  = playerObj.transform;

                // Chest
                var chestObj = new GameObject("chest");
                chestObj.transform.localScale = new Vector3(100, 100);
                var chestSpriteRenderer = chestObj.AddComponent <SpriteRenderer>();
                var chestSpriteSheet    = chestObj.AddComponent <SpriteSheet>();
                chestSpriteRenderer.sortingOrder = 4;
                var chestSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_CHESTS];
                var chestSpriteRow = chestSprite.SliceRow(opts.ChestSpriteIndex).ToArray();
                chestSpriteSheet.SetSheet(chestSpriteRow);
                chestSpriteRenderer.sprite = chestSpriteSheet.WalkSouth[1];
                chestObj.transform.parent  = playerObj.transform;

                // Head
                var headObj = new GameObject("head");
                headObj.transform.localScale = new Vector3(100, 100);
                var headSpriteRenderer = headObj.AddComponent <SpriteRenderer>();
                var headSpriteSheet    = headObj.AddComponent <SpriteSheet>();
                headSpriteRenderer.sortingOrder = 5;
                var headSprite    = AssetHandler.LoadedAssets[DefaultAssets.SPR_HEADS];
                var headSpriteRow = headSprite.SliceRow(opts.HeadSpriteIndex).ToArray();
                headSpriteSheet.SetSheet(headSpriteRow);
                headSpriteRenderer.sprite = headSpriteSheet.WalkSouth[1];
                headObj.transform.parent  = playerObj.transform;

                playerObj.transform.parent = UnityExtensions.GetEntitiesContainer().transform;

                // This gotta be in the end
                playerObj.transform.position = opts.Position.ToUnityPosition();

                var playerWrapper = new PlayerWrapper()
                {
                    PlayerObject = playerObj,
                    EntityType   = EntityType.PLAYER,
                    MoveSpeed    = opts.Speed,
                    Position     = opts.Position,
                    UID          = opts.UserId,
                };

                if (opts.IsMainPlayer)
                {
                    playerWrapper.Behaviour = playerObj.AddComponent <PlayerBehaviour>();
                    playerWrapper.SessionId = UnityClient.Player.SessionId;
                    UnityClient.Player      = playerWrapper;
                }
                else
                {
                    playerWrapper.Behaviour = playerObj.AddComponent <LivingEntityBehaviour>();
                }

                playerWrapper.Behaviour.SpriteSheets.Add(spriteSheet);
                playerWrapper.Behaviour.SpriteSheets.Add(headSpriteSheet);
                playerWrapper.Behaviour.SpriteSheets.Add(legsSpriteSheet);
                playerWrapper.Behaviour.SpriteSheets.Add(chestSpriteSheet);

                playerWrapper.Behaviour.Entity = playerWrapper;

                UnityClient.Map.UpdateEntityPosition(playerWrapper, null, opts.Position);
            }
        }