📄 worldobject.cs
字号:
//---------------------------------------------------------------------
// This file is part of the Microsoft .NET Framework SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Globalization;
using GraphicsLibrary;
using InputLibrary;
namespace GameApp
{
/// <summary>
/// Defines an object within the game world. World objects range
/// from the player and enemies, to static objects like mailboxes.
/// </summary>
public class WorldObject
{
/// <summary>
/// Stores the original collidable flag.
/// </summary>
protected bool ResetCollidable
{
get { return resetCollidableValue; }
set { resetCollidableValue = value; }
}
bool resetCollidableValue;
/// <summary>
/// Stores the original world x location.
/// </summary>
protected float ResetWorldX
{
get { return resetWorldXValue; }
set { resetWorldXValue = value; }
}
float resetWorldXValue;
/// <summary>
/// Stores the original world y location.
/// </summary>
protected float ResetWorldY
{
get { return resetWorldYValue; }
set { resetWorldYValue = value; }
}
float resetWorldYValue;
/// <summary>
/// Defines the world object's state.
/// </summary>
public enum AnimationState
{
None = 0x0000,
Stop = 0x0001,
Right = 0x0002,
Left = 0x0004,
ForceRight = 0x0008,
Dead = 0x0010,
DirectionMask = 0x00ff,
Walk = 0x0100,
Duck = 0x0200,
Attack = 0x0400,
Static = 0x1000,
AnimationMask = 0xff00
}
/// <summary>
/// Current stat of the world object.
/// </summary>
public AnimationState State
{
get { return curState; }
set { curState = value; }
}
AnimationState curState = AnimationState.None;
/// <summary>
/// Get the shot info for this object.
/// </summary>
protected ShotInfo[] GetShotInfo()
{
return shotInfoValue;
}
/// <summary>
/// Set the shot info for this object
/// </summary>
/// <param name="shotInfo"></param>
protected void SetShotInfo(ShotInfo[] shotInfo)
{
shotInfoValue = shotInfo;
}
ShotInfo[] shotInfoValue = null;
/// <summary>
/// Bounds used by static objects - never changes.
/// </summary>
Bounds staticBounds = null;
/// <summary>
/// Defines acknowledged directions within the world.
/// </summary>
protected enum WorldDir
{
Left,
Right,
Up,
Down
}
/// <summary>
/// Types of animations the object can be playing.
/// </summary>
public enum AnimationType
{
Walk = 0,
Attack,
Duck,
Death,
Count
}
/// <summary>
/// Owner of this object. This is used to differentiate shots
/// fired from an object from shots fired by other objects.
/// </summary>
public WorldObject Parent { get { return parentValue; } }
WorldObject parentValue = null;
/// <summary>
/// Specifies if the world object is dead.
/// </summary>
public bool Dead
{
get
{
return (AnimationState.Dead & curState) ==
AnimationState.Dead;
}
}
/// <summary>
/// Get the world object's current bounds. The bounds will vary
/// depending on the current animation state.
/// </summary>
public Bounds Bounds
{
get
{
if ((curState & AnimationState.Static) != 0)
return staticBounds;
if ((curState & AnimationState.Attack) != 0)
return AttackCycle.Bounds;
if ((curState & AnimationState.Duck) != 0)
return DuckCycle.Bounds;
return WalkCycle.Bounds;
}
}
/// <summary>
/// List of animation cycles available to the world object.
/// </summary>
protected AnimationCycle GetAnimationCycle(int index)
{
return animationCycles[index];
}
/// <summary>
/// Sets one of the animation cycles available to a world object
/// </summary>
/// <param name="index">The index of the animation cycle</param>
/// <param name="cycle">The animation cycle to store</param>
protected void SetAnimationCycle(int index, AnimationCycle cycle)
{
animationCycles[index] = cycle;
}
AnimationCycle[] animationCycles =
new AnimationCycle[(int)AnimationType.Count];
/// <summary>
/// Gets the walk animation cycle.
/// </summary>
protected AnimationCycle WalkCycle
{
get { return GetAnimationCycle((int)AnimationType.Walk); }
}
/// <summary>
/// Gets the attack animation cycle.
/// </summary>
protected AnimationCycle AttackCycle
{
get { return GetAnimationCycle((int)AnimationType.Attack); }
}
/// <summary>
/// Gets the duck animation cycle.
/// </summary>
protected AnimationCycle DuckCycle
{
get { return GetAnimationCycle((int)AnimationType.Duck); }
}
/// <summary>
/// Gets the death animation cycle.
/// </summary>
protected AnimationCycle DeathCycle
{
get { return GetAnimationCycle((int)AnimationType.Death); }
}
/// <summary>
/// Specifies if this world object is the player.
/// </summary>
protected bool Player
{
get { return playerValue; }
set { playerValue = value; }
}
bool playerValue = false;
/// <summary>
/// Activation distance of this object. The object will not become
/// active, meaning that it will not be processed, until the player
/// comes within this distance of the object.
/// </summary>
float activateDistance = 0.0F;
/// <summary>
/// AI instance that controls this object.
/// </summary>
AI AILogic = null;
/// <summary>
/// Specifies if this object is active. The object is not processed
/// or drawn unless it is active.
/// </summary>
public bool Active
{
get { return activeValue; }
set { activeValue = value; }
}
bool activeValue = false;
/// <summary>
/// Specifies if this object is dynamic. Dynamic objects are created
/// programmatically and not defined in the level data file.
/// </summary>
public bool Dynamic { get { return dynamicValue; } }
bool dynamicValue = false;
/// <summary>
/// Specifies if the world object should be removed from the level's
/// list of objects and disposed.
/// </summary>
public bool RemoveMe { get { return removeMeValue; } }
bool removeMeValue = false;
/// <summary>
/// Specifies if this object is collidable. If not then it will be
/// ignored when performing collision checks with other objects.
/// </summary>
public bool Collidable
{
get { return collidableValue; }
set { collidableValue = value; }
}
bool collidableValue = false;
/// <summary>
/// X location within the world. Do not confuse this with the screen
/// location of the object.
/// </summary>
public float WorldX
{
get { return worldXValue; }
set { worldXValue = value; }
}
float worldXValue = 0.0F;
/// <summary>
/// Y location within the world. Do not confuse this with the screen
/// location of the object.
/// </summary>
public float WorldY
{
get { return worldYValue; }
set { worldYValue = value; }
}
float worldYValue = 0.0F;
/// <summary>
/// Velocity in the x direction, at which the object is traveling
/// through the world.
/// </summary>
public float VelocityX
{
get { return velocityXValue; }
set { velocityXValue = value; }
}
float velocityXValue = 0.0F;
/// <summary>
/// Velocity in the y direction, at which the object is traveling
/// through the world.
/// </summary>
public float VelocityY
{
get { return velocityYValue; }
set { velocityYValue = value; }
}
float velocityYValue = 0.0F;
/// <summary>
/// Draw width of this object.
/// </summary>
public int Width { get { return (animationValue.CellWidth); } }
/// <summary>
/// Draw options for this object. These options are set before drawing
/// this object.
/// </summary>
protected DrawOptions DrawOptions
{
get { return drawOptionsValue; }
set { drawOptionsValue = value; }
}
DrawOptions drawOptionsValue = 0;
/// <summary>
/// Animation instance which represents this object.
/// </summary>
public Animation Animation
{
get { return animationValue; }
set { animationValue = value; }
}
Animation animationValue = null;
/// <summary>
/// Create an empty instance of a world object. This overload is
/// provided for creating a player object.
/// </summary>
protected WorldObject()
{
}
/// <summary>
/// Initialize a world object with the data specified in the given
/// DataRow.
/// </summary>
/// <param name="dr">DataRow defining the world object</param>
/// <param name="lev">Current level</param>
public WorldObject(DataRow dr, Level lev)
{
Debug.Assert(lev.AnimationList != null && lev.AnimationList.Count > 0,
"WorldObject.WorldObject: No animations loaded");
Debug.Assert(lev.AIList != null && lev.AIList.Count > 0,
"WorldObject.WorldObject: No AI loaded");
for (int i = 0; i < (int)AnimationType.Count; i++)
{
animationCycles[i] = new AnimationCycle();
Debug.Assert(animationCycles[i] != null,
"WorldObject.WorldObject: Failed to allocate " +
"animation cycle");
}
// Walk
WalkCycle.AnimationRate = int.Parse((string)dr["WalkAnimationRate"],
CultureInfo.InvariantCulture);
WalkCycle.StartCell = int.Parse((string)dr["WalkStartCell"],
CultureInfo.InvariantCulture);
WalkCycle.EndCell = int.Parse((string)dr["WalkEndCell"],
CultureInfo.InvariantCulture);
float x = float.Parse((string)dr["WalkBoundsX"],
CultureInfo.InvariantCulture);
float y = float.Parse((string)dr["WalkBoundsY"],
CultureInfo.InvariantCulture);
float r = float.Parse((string)dr["WalkBoundsRadius"],
CultureInfo.InvariantCulture);
WalkCycle.Bounds = new Bounds(x, y, r);
// Attack
AttackCycle.AnimationRate = int.Parse(
(string)dr["AttackAnimationRate"],
CultureInfo.InvariantCulture);
AttackCycle.StartCell = int.Parse((string)dr["AttackStartCell"],
CultureInfo.InvariantCulture);
AttackCycle.EndCell = int.Parse((string)dr["AttackEndCell"],
CultureInfo.InvariantCulture);
x = float.Parse((string)dr["AttackBoundsX"],
CultureInfo.InvariantCulture);
y = float.Parse((string)dr["AttackBoundsY"],
CultureInfo.InvariantCulture);
r = float.Parse((string)dr["AttackBoundsRadius"],
CultureInfo.InvariantCulture);
AttackCycle.Bounds = new Bounds(x, y, r);
// Duck
DuckCycle.AnimationRate = int.Parse(
(string)dr["DuckAnimationRate"],
CultureInfo.InvariantCulture);
DuckCycle.StartCell = int.Parse((string)dr["DuckStartCell"],
CultureInfo.InvariantCulture);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -