Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

The SpaceShoot server

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
27 Mar 2012CPOL28 min read 52.1K   1.9K   56  
Providing a stable and powerful server for the JavaScript / HTML5 browser game SpaceShoot with some gimmicks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Json;
using SpaceShooterServer.Interfaces;

namespace SpaceShooterServer.Game
{
    public class Bomb : GameObject, IParticleCollision, IShipCollision, IAsteroidCollision, IBombCollision, IExplodable, IOwner
    {
        #region Constants

        public const int MAX_BOMB_RADIUS = 300;
        const int BOMB_RESIDUE = 10;
        const int BOMB_LIFE = 10;
        const int BOMB_WAIT = 100;
        const int BOMB_DAMAGE_FACTOR = 120;
        const int BOMB_PHYSICAL_SIZE = 16;

        #endregion

        #region Members

        int life;
        int radius;

        #endregion

        #region ctor

        public Bomb()
        {
            HitList = new List<GameObject>();
            Life = BOMB_LIFE;
            Wait = BOMB_WAIT;
            Size = BOMB_PHYSICAL_SIZE;
        }

        #endregion

        #region Creator

        public static Bomb Create(Ship ship)
        {
            Bomb bomb = new Bomb { Game = ship.Game, X = ship.X, Y = ship.Y, Owner = ship };
            ship.Game.Objects.Add(bomb);
            return bomb;
        }

        #endregion

        #region Properties

        public List<GameObject> HitList { get; set; }
	    public Ship Owner { get; set; }
        public int Wait { get; set; }
        public int ExplosionType { get { return 2; } }
        public bool Physical { get { return Wait > 0; } }

        public override int Damage
        {
            set { }
            get
            {
                if (Physical)
                    return 0;

                var invRadius = MAX_BOMB_RADIUS - radius;
                return (int)((double)(BOMB_DAMAGE_FACTOR * invRadius * invRadius) / (double)(MAX_BOMB_RADIUS * MAX_BOMB_RADIUS)) + BOMB_RESIDUE;
            }
        }

        public override int Size
        {
            get
            {
                return radius * 2;
            }
            set
            {
                radius = value / 2;
            }
        }

	    public int Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                Remove = radius > MAX_BOMB_RADIUS;
            }
        }

        public int Life 
        {
            get { return life; }
            set
            {
                life = value;

                if (life < 1)
                    Dead();
            }
        }

        public override void Logic()
        {
            if (Physical) 
		        Wait--;
            else
                Radius += 5;
        }

        #endregion

        #region Methods

        public override JsonObject ToJson()
        {
            var json = new JsonObject();
            json["owner"] = Owner.Id;
            json["x"] = X;
            json["y"] = Y;
            json["wait"] = Wait;
            json["radius"] = Radius;
            json["remove"] = Remove;
            return json;
        }

        public override bool CannotCollide(GameObject target)
        {
            return HitList.Contains(target);
        }

        public void OnCollision(Ship ship)
        {
            if (Physical)
                return;

            HitList.Add(ship);

            if (ship.Hit(this) && Owner != null)
            {
                if (Owner == ship)
                {
                    InfoText.Add(ship, "-1", 50, ship);
                    Owner.Points--;
                }
                else
                {
                    InfoText.Add(this.Owner, "+1", 50, this);
                    Owner.Points++;
                }
            }
        }

        public void OnCollision(Asteroid asteroid)
        {
            if (Physical)
                return;

            HitList.Add(asteroid);

            if (asteroid.Hit(this))
                InfoText.Add(asteroid, "Good!", 50, this);
        }

        public void OnCollision(Bomb bomb)
        {
            if (Physical && !bomb.Physical)
                bomb.OnCollision(this);
            else if (bomb.Physical && !Physical)
            {
                HitList.Add(bomb);
                bomb.Hit(this);
            }
        }

        public void OnCollision(Particle particle)
        {
            particle.OnCollision(this);
        }

        public override bool Hit(GameObject source)
        {
            if (source.Damage == 0)
                return false;

            Life -= source.Damage;

            if (Remove)
                return true;

            InfoText.Add(this, "-" + source.Damage, 50, source);
            return false;
        }

        #endregion
    }
}

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
Chief Technology Officer
Germany Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

Comments and Discussions