/// <summary> /// Fire a shot from the object's current location. /// </summary> /// <param name="lev">Current level</param> /// <param name="shot">Information regarding shot to be fired</param> /// <param name="dir">Direction to fire shot</param> /// <param name="xMultiplier">Velocity x multiplier</param> /// <param name="yMultiplier">Velocity y multiplier</param> protected void FireShot(Level lev, ShotInfo shot, WorldDir dir, float xMultiplier, float yMultiplier) { shot.Fired = true; WorldObject wo = new WorldObject( (Animation)lev.AnimationList[shot.AnimationId], shot.AnimationRate); Debug.Assert(wo != null, "Player.Update: Failed to create player bullet"); float velX = shot.VelocityX * xMultiplier; float offsetX = shot.XOffset; if (dir == WorldDir.Left) { velX = -velX; offsetX = -offsetX; } wo.VelocityX = velX; wo.VelocityY = shot.VelocityY * yMultiplier; wo.WorldX = worldXValue + offsetX; wo.WorldY = worldYValue + shot.YOffset; wo.SetTransparency(shot.UseTransparency); wo.staticBounds = new Bounds(0.0F, 0.0F, shot.Radius); wo.parentValue = this; if (shot.UseGravity) { wo.AILogic = AIHandler.Create(AI.AIType.ShotArc); } lev.WorldObjects.Add(wo); }
/// <summary> /// Initialize the level. /// </summary> /// <param name="ds">DataSet containing level data</param> /// <param name="graphics">Valid Graphics object</param> public Level(DataSet ds, IGraphics graphics) { // Store graphics Debug.Assert(graphics != null, "Level.Level: Invalid Graphics object"); this.graphics = graphics; // Validate the DataSet Debug.Assert(ds != null && ds.Tables != null, "Level.Level: Invalid DataSet"); // General DataTable dt = ds.Tables["General"]; DataRow drGen = dt.Rows[0]; screenEdgeBufferValue = float.Parse( (string)(drGen["ScreenEdgeBuffer"]), CultureInfo.InvariantCulture); FrameRateCheckRateValue = float.Parse( (string)(drGen["FrameRateCheckRate"]), CultureInfo.InvariantCulture); gravityValue = float.Parse((string)(drGen["Gravity"]), CultureInfo.InvariantCulture); // Images Debug.Assert(ds.Tables["Image"] != null && ds.Tables["Image"].Rows != null, "Level.Level: No images specified in level data"); dt = ds.Tables["Image"]; foreach (DataRow dr in dt.Rows) { IBitmap bmp = graphics.CreateBitmap(GameMain.GetFullPath( @"Data\Level\" + (string)dr["FileName"]), bool.Parse((string)dr["Transparency"])); Debug.Assert(bmp != null, string.Format(CultureInfo.InvariantCulture, "Failed to initialize bitmap {0}", @"Data\Level\" + (string)dr["FileName"])); images.Add(bmp); } // Layers dt = ds.Tables["Layer"]; foreach (DataRow dr in dt.Rows) { layers.Add(new Layer(dr, images)); } Debug.Assert(layers.Count >= 1, "Level does not contain 2 or more layers"); ds = null; // AI DataSet dsAI = new DataSet(); Debug.Assert(dsAI != null && dsAI.Tables != null, "Level.Level: Failed to initialize AI DataSet"); dsAI.Locale = CultureInfo.InvariantCulture; dsAI.ReadXml(GameMain.GetFullPath(@"Data\AI\ai.xml")); dt = dsAI.Tables["Definition"]; Debug.Assert(dt != null && dt.Rows != null, "Level.Level: Failed to load AI DataTable"); foreach (DataRow dr in dt.Rows) { AI ai = AIHandler.Create(dr); Debug.Assert(ai != null, "Level.Level: Failed to initialize AI"); this.ai.Add(ai); } dsAI = null; DataSet dsAnimations = new DataSet(); Debug.Assert(dsAnimations != null && dsAnimations.Tables != null, "Level.Level: Failed to initialize animation DataSet"); dsAnimations.Locale = CultureInfo.InvariantCulture; // Animations dsAnimations.ReadXml(GameMain.GetFullPath(@"Data\Animations\animations.xml")); dt = dsAnimations.Tables["Definition"]; Debug.Assert(dt != null && dt.Rows != null, "Level.Level: Failed to load animation DataTable"); foreach (DataRow dr in dt.Rows) { int numberRows = int.Parse((string)dr["Rows"], CultureInfo.InvariantCulture); int numberColumns = int.Parse((string)dr["Columns"], CultureInfo.InvariantCulture); int cellWidth = int.Parse((string)dr["CellWidth"], CultureInfo.InvariantCulture); int cellHeight = int.Parse((string)dr["CellHeight"], CultureInfo.InvariantCulture); Animation animation = new Animation(GameMain.GetFullPath( @"Data\Animations\" + (string)dr["FileName"]), graphics, numberRows, numberColumns, 0, cellWidth, cellHeight, 0); Debug.Assert(animation != null && animation.Init, "Level.Level: Failed to load animation"); animations.Add(animation); } dsAnimations = null; // Player DataSet dsPlayer = new DataSet(); Debug.Assert(dsPlayer != null, "Level.Level: Failed to initialize player DataSet"); dsPlayer.Locale = CultureInfo.InvariantCulture; dsPlayer.ReadXml(GameMain.GetFullPath(@"Data\Player\player.xml")); Player p = new Player(dsPlayer, graphics, animations); Debug.Assert(p != null, "Level.Level: Failed to initialize player"); worldObjectsValue.Add(p); dsPlayer = null; // World Objects DataSet dsWorldObjects = new DataSet(); Debug.Assert(dsWorldObjects != null && dsWorldObjects.Tables != null, "Level.Level: Failed to initialize world object DataSet"); dsWorldObjects.Locale = CultureInfo.InvariantCulture; dsWorldObjects.ReadXml(GameMain.GetFullPath(@"Data\WorldObjects\worldobjects.xml")); dt = dsWorldObjects.Tables["Instance"]; if (dt != null) { foreach (DataRow dr in dt.Rows) { WorldObject wo = new WorldObject(dr, this); Debug.Assert(wo != null, "Level.Level: Failed to initialize world object"); worldObjectsValue.Add(wo); } } }
/// <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); DuckCycle.EndCell = int.Parse((string)dr["DuckEndCell"], CultureInfo.InvariantCulture); x = float.Parse((string)dr["DuckBoundsX"], CultureInfo.InvariantCulture); y = float.Parse((string)dr["DuckBoundsY"], CultureInfo.InvariantCulture); r = float.Parse((string)dr["DuckBoundsRadius"], CultureInfo.InvariantCulture); DuckCycle.Bounds = new Bounds(x, y, r); // Death DeathCycle.AnimationRate = int.Parse( (string)dr["DeathAnimationRate"], CultureInfo.InvariantCulture); DeathCycle.StartCell = int.Parse((string)dr["DeathStartCell"], CultureInfo.InvariantCulture); DeathCycle.EndCell = int.Parse((string)dr["DeathEndCell"], CultureInfo.InvariantCulture); // General info worldXValue = float.Parse((string)dr["X"], CultureInfo.InvariantCulture); worldYValue = float.Parse((string)dr["Y"], CultureInfo.InvariantCulture); activateDistance = float.Parse((string)dr["ActivateDistance"], CultureInfo.InvariantCulture); collidableValue = bool.Parse((string)dr["Collidable"]); // Animation int animationId = int.Parse((string)dr["AnimationId"], CultureInfo.InvariantCulture); Debug.Assert(animationId >= 0 && animationId < lev.AnimationList.Count, "WorldObject.WorldObject: Animatin ID out of range"); animationValue = new Animation( (Animation)lev.AnimationList[animationId], 0, 0); Debug.Assert(animationValue != null && animationValue.Init, "WorldObject.WorldObject: Failed to initialize animation"); // AI int aiId = int.Parse((string)dr["AIId"], CultureInfo.InvariantCulture); if (aiId >= 0) { Debug.Assert(aiId < lev.AIList.Count, "WorldObject.WorldObject: AI Id out of range"); AILogic = AIHandler.Create((AI)lev.AIList[aiId]); AILogic.Init(this, lev); } // Draw options if (bool.Parse((string)dr["Transparency"])) { DrawOptions = DrawOptions.BlitKeyedTransparency; } // Shot, if one exists int numShots = 0; if (!dr.IsNull("Shot2AnimationId")) { numShots = 2; } else if (!dr.IsNull("Shot1AnimationId")) { numShots = 1; } if (numShots > 0) { shotInfoValue = new ShotInfo[numShots]; for (int i = 0; i < shotInfoValue.Length; i++) { shotInfoValue[i] = new ShotInfo(dr, i + 1); ShotInfo shot = shotInfoValue[i]; Debug.Assert(shot != null, "WorldObject.WorldObject: Failed to allocate " + "shot info"); } } ResetCollidable = collidableValue; ResetWorldX = worldXValue; ResetWorldY = worldYValue; }