// Creates a new character // <characterClass> is the character's class (gunner, support, tank) // <maxHealth> is the character's starting health // <position> is the character's starting position public Character(CharacterClass characterClass, int maxHealth, Vector3 position) { if (maxHealth < 0) { throw new Exception("Invalid character max health!"); } Class = characterClass; HeldWeapon = new WeaponKnife(); Armor = false; MaxHealth = maxHealth; Health = maxHealth; Score = 0; Cooldown = 0; CooldownTimer = new Timer(); CooldownTimer.Elapsed += new ElapsedEventHandler(RunCooldown); CooldownTimer.Interval = 100; CooldownTimer.Enabled = false; Velocity = new Vector2(); Position = position; switch (Class) { case CharacterClass.Gunner: PlayerImage1 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png2/attack1/2_Special_forces_attack_Attack1_005.png"); PlayerImage2 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png2/attack/2_Special_forces_attack_Attack_000_center.png"); PlayerSpriteJump = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png2/jump/2_Special_forces_Jump_003.png"); break; case CharacterClass.Support: PlayerImage1 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png1/attack1/1_Special_forces_attack_Attack1_005.png"); PlayerImage2 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png1/attack/1_Special_forces_attack_Attack_000_center.png"); PlayerSpriteJump = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png1/jump/1_Special_forces_Jump_003.png"); break; case CharacterClass.Tank: PlayerImage1 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png3/attack1/3_Special_forces_Attack1_003.png"); PlayerImage2 = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png3/attack/3_Special_forces_Attack_002_center.png"); PlayerSpriteJump = GameApp.Instance.ResourceCache.GetSprite2D("characters/special_forces/png3/jump/3_Special_forces_Jump_003.png"); break; } PlayerSpriteAttack = PlayerImage1; }
// This method loads the player character // Recieves a line of serialized player data private void LoadPlayer(string line) { InvokeOnMain(() => { // Create variables to store character properties. // Order of serialized is: // Class,HeldWeapon,Armor,Health,MaxHealth,Score,X,Y,Z string[] props = line.Split(','); var playerClass = props[0]; var playerWeapon = props[1]; var playerArmor = props[2]; var playerHealth = props[3]; var playerMaxHealth = props[4]; var playerScore = props[5]; string[] position = props.Skip(6).Take(3).ToArray(); float x = float.Parse(position[0], CultureInfo.InvariantCulture.NumberFormat); float y = float.Parse(position[1], CultureInfo.InvariantCulture.NumberFormat); float z = float.Parse(position[2], CultureInfo.InvariantCulture.NumberFormat); // Determine CharacterClass switch (playerClass) { case "Gunner": charClass = CharacterClass.Gunner; break; case "Support": charClass = CharacterClass.Support; break; case "Tank": charClass = CharacterClass.Tank; break; } // Create default player with correct class CreatePlayer(x, y); // Determine HeldWeapon Weapon heldWeapon = new WeaponKnife(); switch (playerWeapon) { case "Royale_Platformer.Model.WeaponKnife": heldWeapon = new WeaponKnife(); break; case "Royale_Platformer.Model.WeaponPistol": heldWeapon = new WeaponPistol(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; case "Royale_Platformer.Model.WeaponPistolShield": heldWeapon = new WeaponPistolShield(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; case "Royale_Platformer.Model.WeaponShotgun": heldWeapon = new WeaponShotgun(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; case "Royale_Platformer.Model.WeaponAdvancedShotgun": heldWeapon = new WeaponAdvancedShotgun(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; case "Royale_Platformer.Model.WeaponAR": heldWeapon = new WeaponAR(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; case "Royale_Platformer.Model.WeaponAdvancedAR": heldWeapon = new WeaponAdvancedAR(); PlayerCharacter.PlayerSpriteAttack = PlayerCharacter.PlayerImage2; break; } // Update Player PlayerCharacter.MaxHealth = Convert.ToInt32(playerMaxHealth); PlayerCharacter.Position = new Vector3(x, y, z); PlayerCharacter.Health = Convert.ToInt32(playerHealth); PlayerCharacter.HeldWeapon = heldWeapon; PlayerCharacter.Armor = playerArmor == "True" ? true : false; PlayerCharacter.Score = Convert.ToInt32(playerScore); // Update Camera cameraNode.Parent = PlayerCharacter.WorldNode; cameraNode.Position = new Vector3(x, y, -1); }); }
// This method loads the enemy characters // Recieves a line of serialized character data private void LoadEnemies(string line) { InvokeOnMain(() => { // Load each enemy string[] enemiesSplit = line.Split(';'); foreach (var enemy in enemiesSplit.Take(enemiesSplit.Length - 1)) { // Create variables to store character properties. // Order of serialized is: // Class,HeldWeapon,Armor,Health,MaxHealth,Score,X,Y,Z string[] props = enemy.Split(','); var enemyClass = props[0]; var enemyWeapon = props[1]; var enemyArmor = props[2]; var enemyHealth = props[3]; var enemyMaxHealth = props[4]; var enemyScore = props[5]; string[] position = props.Skip(6).Take(3).ToArray(); float x = float.Parse(position[0], CultureInfo.InvariantCulture.NumberFormat); float y = float.Parse(position[1], CultureInfo.InvariantCulture.NumberFormat); float z = float.Parse(position[2], CultureInfo.InvariantCulture.NumberFormat); // Determine CharacterClass CharacterClass charClass = CharacterClass.Gunner; switch (enemyClass) { case "Gunner": charClass = CharacterClass.Gunner; break; case "Support": charClass = CharacterClass.Support; break; case "Tank": charClass = CharacterClass.Tank; break; } // Determine HeldWeapon Weapon heldWeapon = new WeaponKnife(); switch (enemyWeapon) { case "Royale_Platformer.Model.WeaponKnife": heldWeapon = new WeaponKnife(); break; case "Royale_Platformer.Model.WeaponPistol": heldWeapon = new WeaponPistol(); break; case "Royale_Platformer.Model.WeaponPistolShield": heldWeapon = new WeaponPistolShield(); break; case "Royale_Platformer.Model.WeaponShotgun": heldWeapon = new WeaponShotgun(); break; case "Royale_Platformer.Model.WeaponAdvancedShotgun": heldWeapon = new WeaponAdvancedShotgun(); break; case "Royale_Platformer.Model.WeaponAR": heldWeapon = new WeaponAR(); break; } // Create new Character CharacterEnemy enemyPlayer = new CharacterEnemy( charClass, Convert.ToInt32(enemyMaxHealth), new Vector3(x, y, z) ); enemyPlayer.Health = Convert.ToInt32(enemyHealth); enemyPlayer.HeldWeapon = heldWeapon; enemyPlayer.Armor = enemyArmor == "True" ? true : false; enemyPlayer.Score = Convert.ToInt32(enemyScore); Sprite2D shieldSprite = ResourceCache.GetSprite2D("shield.png"); // Load Enemy //AnimationSet2D sprite = ResourceCache.GetAnimationSet2D(enemyPlayer.GetSprite()); Sprite2D sprite = ResourceCache.GetSprite2D(enemyPlayer.GetSprite()); if (sprite == null) { throw new Exception("Enemy sprite not found"); } enemyPlayer.CreateNode(scene, sprite, shieldSprite, new Vector2(enemyPlayer.Position.X, enemyPlayer.Position.Y)); Characters.Add(enemyPlayer); } }); }