/// <summary> /// Loads the room into memory. /// If IsPersistant is set to true, loads up the previously saved State file /// </summary> /// <param name="doorIndex">Represents which door the player is entering from. /// If set to -1, player will enter at the default spawning location.</param> public void Load(string filePath, int doorIndex) { if (filePath != "" && filePath != null) { try { int numberOfDoors; int numberOfEnemies; int numberOfEnvironmentObjects; using (Stream stream = TitleContainer.OpenStream(filePath)) { using (StreamReader sr = new StreamReader(stream)) { longName = GameResources.getNextDataLine(sr, "#"); graphicsName = GameResources.getNextDataLine(sr, "#"); spawnLocation = new Vector2(Convert.ToInt16(GameResources.getNextDataLine(sr, "#")), Convert.ToInt16(GameResources.getNextDataLine(sr, "#"))); isPersistant = bool.Parse(GameResources.getNextDataLine(sr, "#")); hasLighting = bool.Parse(GameResources.getNextDataLine(sr, "#")); musicName = GameResources.getNextDataLine(sr, "#"); numberOfDoors = Convert.ToInt16(GameResources.getNextDataLine(sr, "#")); doorArray = new Door[numberOfDoors]; for (int i = 0; i < numberOfDoors; i++) { Door.DoorOrientations orientation = (Door.DoorOrientations)byte.Parse(GameResources.getNextDataLine(sr, "#")); string roomName = GameResources.getNextDataLine(sr, "#"); int connectedDoorIndex = Convert.ToInt16(GameResources.getNextDataLine(sr, "#")); Locks lockType = (Locks)byte.Parse(GameResources.getNextDataLine(sr, "#")); doorArray[i] = new Door(Content, soundEngine, orientation, roomName, connectedDoorIndex, lockType); doorArray[i].Spawn(new Vector2(float.Parse(GameResources.getNextDataLine(sr, "#")), float.Parse(GameResources.getNextDataLine(sr, "#")))); } numberOfEnemies = Int16.Parse(GameResources.getNextDataLine(sr, "#")); enemyArray = new Enemy[numberOfEnemies]; for (int i = 0; i < numberOfEnemies; i++) { String enemyName = GameResources.getNextDataLine(sr, "#"); enemyArray[i] = new Enemy(enemyName, Content); enemyArray[i].Spawn(new Vector2(Int16.Parse(GameResources.getNextDataLine(sr, "#")), Int16.Parse(GameResources.getNextDataLine(sr, "#")))); } numberOfEnvironmentObjects = Int16.Parse(GameResources.getNextDataLine(sr, "#")); environmentArray = new PhysicsObject[numberOfEnvironmentObjects]; for (int i = 0; i < numberOfEnvironmentObjects; i++) { String objectName = GameResources.getNextDataLine(sr, "#"); bool objHasGraphics = bool.Parse(GameResources.getNextDataLine(sr, "#")); environmentArray[i] = new PhysicsObject(objectName, Content, new Vector2(0,0), objHasGraphics, soundEngine, false); environmentArray[i].IsSolid = bool.Parse(GameResources.getNextDataLine(sr, "#")); environmentArray[i].Spawn(new Vector2(Int16.Parse(GameResources.getNextDataLine(sr, "#")), Int16.Parse(GameResources.getNextDataLine(sr, "#")))); } objectArray = new GameObject[numberOfDoors + numberOfEnemies + numberOfEnvironmentObjects]; int arrayIndex = 0; for (int i = 0; i < numberOfDoors; i++) { objectArray[i + arrayIndex] = doorArray[i + arrayIndex]; } arrayIndex += numberOfDoors; for (int i = 0; i < numberOfEnemies; i++) { objectArray[i + arrayIndex] = enemyArray[i]; } arrayIndex += numberOfEnemies; for (int i = 0; i < numberOfEnvironmentObjects; i++) { objectArray[i + arrayIndex] = environmentArray[i]; } if (doorIndex != -1) { spawnLocation = doorArray[doorIndex].Position; if (doorArray[doorIndex].Orientation == Door.DoorOrientations.FacingLeft) spawnLocation.X -= Player.PLAYER_WIDTH; else spawnLocation.X += doorArray[doorIndex].HitBox.Width; } sr.Close(); isLoaded = true; } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("An error occurred: " + e.Message); } } }
/// <summary> /// Initiate damage and flinching caused by collision with a specific /// enemy object from a specific direction. /// </summary> /// <param name="enemy">The enemy to collide with.</param> /// <param name="direction">The side of the player that the enemy is touching.</param> private void CollideWithEnemy(Enemy enemy, XDirection direction) { soundEngine.Play(AudioEngine.SoundEffects.Hurt); currentHealth = Math.Max(0, currentHealth - enemy.ContactDamage); if (direction == XDirection.Left) { velocityX = KNOCK_BACK_V; accelerationX = STOP_DEC; } else { velocityX = -KNOCK_BACK_V; accelerationX = -STOP_DEC; } velocityLimitX = 0; accelerationY = 0; LatestXArrow = XDirection.None; isGravityAffected = true; if (currentHealth > 0) damageStatus = INVINCIBLE_START; else { velocityY -= 500; deathSequenceEndTime = DEATH_SEQUENCE_END_WITH_RAGDOLL; } }