Click here to Skip to main content
15,886,422 members
Articles / Game Development

Learning XNA 2D Engine IceCream With 1945 Demo Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
8 Aug 2012CPOL16 min read 66.5K   2.3K   51  
IceCream1945 is a demonstration of XNA and the IceCream 2D library in a 2D top-down scrolling shooter similar to 1942 for the NES.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IceCream.Components;
using IceCream.Attributes;
using Microsoft.Xna.Framework;
using IceCream;
using Microsoft.Xna.Framework.Graphics;
using IceCream.SceneItems;
using IceCream.SceneItems.AnimationClasses;
using IceCream1945.Global;

namespace IceCream1945.Components
{
    [IceComponentAttribute("BulletComponent")]
    public class BulletComponent : IceComponent
    {
        [IceComponentProperty("Can Damage Player")]
        public bool CanDamagePlayer { get; set; }

        [IceComponentProperty("Can Damange Enemies")]
        public bool CanDamageEnemy { get; set; }

        public BulletComponent() {
        }
        public override void CopyValuesTo(object target) {
            base.CopyValuesTo(target);
            if (target is BulletComponent) {
                BulletComponent targetCom = target as BulletComponent;
                targetCom.CanDamagePlayer = this.CanDamagePlayer;
                targetCom.CanDamageEnemy = this.CanDamageEnemy;
            }
        }
        public override void OnRegister() {
            Enabled = true;
        }

        public override void Update(float elapsedTime) {
            //We get all scene items ( not recommended in bigger games, at least not every frame)
            for (int i=0; i < GlobalGameData.ActiveSceneItems.Count; ++i) {
                SceneItem si = GlobalGameData.ActiveSceneItems[i];

                if (si != Owner && (si.GetType() == typeof(Sprite) || si.GetType() == typeof(AnimatedSprite)) && !Owner.MarkForDelete) {
                    if (CanDamageEnemy) {
                        if (si.CheckType(2) && Owner.BoundingRect.Intersects(si.BoundingRect)) {
                            si.MarkForDelete = true;
                            Owner.MarkForDelete = true;
                            GlobalGameData.ActiveSceneItems.Remove(si);
                            GlobalGameData.ActiveSceneItems.Remove(Owner);
                            --i;

                            AnimatedSprite explosion = Owner.SceneParent.CreateCopy<AnimatedSprite>("BigExplosion");
                            explosion.Visible = true;
                            explosion.Position = si.Position;
                            explosion.PositionX += si.BoundingRectSize.X / 2;
                            explosion.PositionY += si.BoundingRectSize.Y / 2;
                            explosion.CurrentAnimation.LoopMax = 1;
                            explosion.CurrentAnimation.HideWhenStopped = true;
                            Owner.SceneParent.RegisterSceneItem(explosion);

                            GlobalGameData.PlayerOnePointTracking.AddScore(50);

                            Sound.Play(Sound.ExplosionHit);
                        }
                    }
                    if (CanDamagePlayer) {
                        if (Owner.BoundingRect.Intersects(GlobalGameData.PlayerAnimatedSprite.BoundingRect)) {
                            --GlobalGameData.PlayerHealth;
                            Owner.MarkForDelete = true;

                            GlobalGameData.ScreenDamageEffect.Reset();//if the player is getting bombarded, we want many flashes
                            GlobalGameData.ScreenDamageEffect.Play();

                            Sound.Play(Sound.Quick_Hit);
                        }
                    }
                }
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions