Click here to Skip to main content
15,896,118 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 67.3K   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 IceCream.Components;
using IceCream.Attributes;
using IceCream.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using IceCream.SceneItems;
using IceCream1945.Components;
using IceCream.Utility;
using IceCream1945.Global;

namespace IceCream1945.Components
{
    [IceComponentAttribute("PlayerControllableComponent")]
    public class PlayerControllableComponent : IceComponent
    {
        // PlayerIndex adn Velocity are set in MilkShake when the Component is added to it's owner.
        // represents the player index, each player has different controls
        [IceComponentProperty("PlayerIndex")]
        public PlayerIndex Playerindex { get; set; }

        // sets the velocity of the current player
        [IceComponentProperty("Velocity")]
        public Vector2 Velocity { get; set; }

        public PlayerControllableComponent() {

        }

        public override void CopyValuesTo(object target) {
            // copy values which are not set in the OnRegister() method
            base.CopyValuesTo(target);
            PlayerControllableComponent component = target as PlayerControllableComponent;
            component.Playerindex = this.Playerindex;
            component.Velocity = this.Velocity;
        }

        public override void OnRegister() {
            Enabled = true;
        }

        public override void Update(float elapsedTime) {
            // when the owner uses PlayerIndex.One
            if (Playerindex == PlayerIndex.One) {
                // if W button is pressed
                if (InputCore.IsKeyDown(Keys.W)) {
                    // we go upwards
                    Owner.PositionY -= Velocity.Y;
                }
                // if S key is pressed
                if (InputCore.IsKeyDown(Keys.S)) {
                    // we go downwards
                    Owner.PositionY += Velocity.Y;
                }
                // if A button is pressed
                if (InputCore.IsKeyDown(Keys.A)) {
                    // we go to the left
                    Owner.PositionX -= Velocity.X;
                }
                // if D button is pressed
                if (InputCore.IsKeyDown(Keys.D)) {
                    // we go to the right
                    Owner.PositionX += Velocity.X;
                }

                if (BulletTimer.Stopwatch(100) && InputCore.IsKeyDown(Keys.Space)) {
                    //fire projectile
                    AnimatedSprite newBullet = Owner.SceneParent.CreateCopy<AnimatedSprite>("FlamingBullet");
                    newBullet.Visible = true;
                    newBullet.Position = Owner.Position;
                    newBullet.PositionX += Owner.BoundingRectSize.X / 2;
                    VelocityComponent velocityCom = newBullet.GetComponent<VelocityComponent>();
                    velocityCom.Enabled = true;
                    Owner.SceneParent.RegisterSceneItem(newBullet);
                    Sound.Play(Sound.Laser_Shoot_Player);
                }

                if (BombTimer.Stopwatch(100) && InputCore.IsKeyDown(Keys.LeftShift)) {
                    //fire bomb
                    Sprite newBullet = Owner.SceneParent.CreateCopy<Sprite>("Bomb");
                    newBullet.Visible = true;
                    newBullet.Position = Owner.Position;
                    newBullet.PositionX += Owner.BoundingRectSize.X / 2;
                    //VelocityComponent velocityCom = newBullet.GetComponent<VelocityComponent>();
                    //velocityCom.Enabled = true;
                    Owner.SceneParent.RegisterSceneItem(newBullet);
                }
            }
            // when the owner uses PlayerIndex.Two
            else if (Playerindex == PlayerIndex.Two) {
                // if Up key is pressed
                if (InputCore.IsKeyDown(Keys.Up)) {
                    // we go upwards
                    Owner.PositionY -= Velocity.Y;
                }
                // if Down key is pressed
                if (InputCore.IsKeyDown(Keys.Down)) {
                    // we go downwards
                    Owner.PositionY += Velocity.Y;
                }
                // if Left key is pressed
                if (InputCore.IsKeyDown(Keys.Left)) {
                    // we go to the left
                    Owner.PositionX -= Velocity.X;
                }
                // if right key is pressed
                if (InputCore.IsKeyDown(Keys.Right)) {
                    // we go to the right
                    Owner.PositionX += Velocity.X;
                }
            }

            if (InputCore.IsKeyDown(Keys.Escape)) {
                GlobalGameData.ShouldQuit = true;
            }

            if (InputCore.IsKeyDown(Keys.Tab)) {
                
            }
        }

        private IceTimer BulletTimer = new IceTimer();
        private IceTimer BombTimer = new IceTimer();
    }
}

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