123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Character.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Diagnostics;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- #endregion
- namespace RolePlayingGameData
- {
- /// <summary>
- /// A character in the game world.
- /// </summary>
- #if !XBOX
- [DebuggerDisplay("Name = {name}")]
- #endif
- public abstract class Character : WorldObject
- {
- #region Character State
- /// <summary>
- /// The state of a character.
- /// </summary>
- public enum CharacterState
- {
- /// <summary>
- /// Ready to perform an action, and playing the idle animation
- /// </summary>
- Idle,
- /// <summary>
- /// Walking in the world.
- /// </summary>
- Walking,
- /// <summary>
- /// In defense mode
- /// </summary>
- Defending,
- /// <summary>
- /// Performing Dodge Animation
- /// </summary>
- Dodging,
- /// <summary>
- /// Performing Hit Animation
- /// </summary>
- Hit,
- /// <summary>
- /// Dead, but still playing the dying animation.
- /// </summary>
- Dying,
- /// <summary>
- /// Dead, with the dead animation.
- /// </summary>
- Dead,
- }
- /// <summary>
- /// The state of this character.
- /// </summary>
- private CharacterState state = CharacterState.Idle;
- /// <summary>
- /// The state of this character.
- /// </summary>
- [ContentSerializerIgnore]
- public CharacterState State
- {
- get { return state; }
- set { state = value; }
- }
- /// <summary>
- /// Returns true if the character is dead or dying.
- /// </summary>
- public bool IsDeadOrDying
- {
- get
- {
- return ((State == CharacterState.Dying) ||
- (State == CharacterState.Dead));
- }
- }
- #endregion
- #region Map Data
- /// <summary>
- /// The position of this object on the map.
- /// </summary>
- private Point mapPosition;
- /// <summary>
- /// The position of this object on the map.
- /// </summary>
- [ContentSerializerIgnore]
- public Point MapPosition
- {
- get { return mapPosition; }
- set { mapPosition = value; }
- }
- /// <summary>
- /// The orientation of this object on the map.
- /// </summary>
- private Direction direction;
- /// <summary>
- /// The orientation of this object on the map.
- /// </summary>
- [ContentSerializerIgnore]
- public Direction Direction
- {
- get { return direction; }
- set { direction = value; }
- }
- #endregion
- #region Graphics Data
- /// <summary>
- /// The animating sprite for the map view of this character.
- /// </summary>
- private AnimatingSprite mapSprite;
- /// <summary>
- /// The animating sprite for the map view of this character.
- /// </summary>
- [ContentSerializer(Optional = true)]
- public AnimatingSprite MapSprite
- {
- get { return mapSprite; }
- set { mapSprite = value; }
- }
- /// <summary>
- /// The animating sprite for the map view of this character as it walks.
- /// </summary>
- /// <remarks>
- /// If this object is null, then the animations are taken from MapSprite.
- /// </remarks>
- private AnimatingSprite walkingSprite;
- /// <summary>
- /// The animating sprite for the map view of this character as it walks.
- /// </summary>
- /// <remarks>
- /// If this object is null, then the animations are taken from MapSprite.
- /// </remarks>
- [ContentSerializer(Optional=true)]
- public AnimatingSprite WalkingSprite
- {
- get { return walkingSprite; }
- set { walkingSprite = value; }
- }
- /// <summary>
- /// Reset the animations for this character.
- /// </summary>
- public virtual void ResetAnimation(bool isWalking)
- {
- state = isWalking ? CharacterState.Walking : CharacterState.Idle;
- if (mapSprite != null)
- {
- if (isWalking && mapSprite["Walk" + Direction.ToString()] != null)
- {
- mapSprite.PlayAnimation("Walk", Direction);
- }
- else
- {
- mapSprite.PlayAnimation("Idle", Direction);
- }
- }
- if (walkingSprite != null)
- {
- if (isWalking && walkingSprite["Walk" + Direction.ToString()] != null)
- {
- walkingSprite.PlayAnimation("Walk", Direction);
- }
- else
- {
- walkingSprite.PlayAnimation("Idle", Direction);
- }
- }
- }
- /// <summary>
- /// The small blob shadow that is rendered under the characters.
- /// </summary>
- private Texture2D shadowTexture;
- /// <summary>
- /// The small blob shadow that is rendered under the characters.
- /// </summary>
- [ContentSerializerIgnore]
- public Texture2D ShadowTexture
- {
- get { return shadowTexture; }
- set { shadowTexture = value; }
- }
- #endregion
- #region Standard Animation Data
- /// <summary>
- /// The default idle-animation interval for the animating map sprite.
- /// </summary>
- private int mapIdleAnimationInterval = 200;
- /// <summary>
- /// The default idle-animation interval for the animating map sprite.
- /// </summary>
- [ContentSerializer(Optional=true)]
- public int MapIdleAnimationInterval
- {
- get { return mapIdleAnimationInterval; }
- set { mapIdleAnimationInterval = value; }
- }
- /// <summary>
- /// Add the standard character idle animations to this character.
- /// </summary>
- private void AddStandardCharacterIdleAnimations()
- {
- if (mapSprite != null)
- {
- mapSprite.AddAnimation(new Animation("IdleSouth", 1, 6,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleSouthwest", 7, 12,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleWest", 13, 18,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleNorthwest", 19, 24,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleNorth", 25, 30,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleNortheast", 31, 36,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleEast", 37, 42,
- MapIdleAnimationInterval, true));
- mapSprite.AddAnimation(new Animation("IdleSoutheast", 43, 48,
- MapIdleAnimationInterval, true));
- }
- }
- /// <summary>
- /// The default walk-animation interval for the animating map sprite.
- /// </summary>
- private int mapWalkingAnimationInterval = 80;
- /// <summary>
- /// The default walk-animation interval for the animating map sprite.
- /// </summary>
- [ContentSerializer(Optional = true)]
- public int MapWalkingAnimationInterval
- {
- get { return mapWalkingAnimationInterval; }
- set { mapWalkingAnimationInterval = value; }
- }
- /// <summary>
- /// Add the standard character walk animations to this character.
- /// </summary>
- private void AddStandardCharacterWalkingAnimations()
- {
- AnimatingSprite sprite = (walkingSprite == null ? mapSprite : walkingSprite);
- if (sprite != null)
- {
- sprite.AddAnimation(new Animation("WalkSouth", 1, 6,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkSouthwest", 7, 12,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkWest", 13, 18,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkNorthwest", 19, 24,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkNorth", 25, 30,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkNortheast", 31, 36,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkEast", 37, 42,
- MapWalkingAnimationInterval, true));
- sprite.AddAnimation(new Animation("WalkSoutheast", 43, 48,
- MapWalkingAnimationInterval, true));
- }
- }
- #endregion
- #region Content Type Reader
- /// <summary>
- /// Reads a Character object from the content pipeline.
- /// </summary>
- public class CharacterReader : ContentTypeReader<Character>
- {
- /// <summary>
- /// Reads a Character object from the content pipeline.
- /// </summary>
- protected override Character Read(ContentReader input,
- Character existingInstance)
- {
- Character character = existingInstance;
- if (character == null)
- {
- throw new ArgumentNullException("existingInstance");
- }
- input.ReadRawObject<WorldObject>(character as WorldObject);
- character.MapIdleAnimationInterval = input.ReadInt32();
- character.MapSprite = input.ReadObject<AnimatingSprite>();
- if (character.MapSprite != null)
- {
- character.MapSprite.SourceOffset =
- new Vector2(
- character.MapSprite.SourceOffset.X - 32,
- character.MapSprite.SourceOffset.Y - 32);
- }
- character.AddStandardCharacterIdleAnimations();
- character.MapWalkingAnimationInterval = input.ReadInt32();
- character.WalkingSprite = input.ReadObject<AnimatingSprite>();
- if (character.WalkingSprite != null)
- {
- character.WalkingSprite.SourceOffset =
- new Vector2(
- character.WalkingSprite.SourceOffset.X - 32,
- character.WalkingSprite.SourceOffset.Y - 32);
- }
- character.AddStandardCharacterWalkingAnimations();
- character.ResetAnimation(false);
- character.shadowTexture = input.ContentManager.Load<Texture2D>(
- @"Textures\Characters\CharacterShadow");
- return character;
- }
- }
- #endregion
- }
- }
|