Click here to Skip to main content
15,891,473 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 52K   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;
using SpaceShooterServer.Structures;

namespace SpaceShooterServer.Game
{
    public class Particle : GameObject, IShipCollision, IAsteroidCollision, IBombCollision, IOwner, IVelocity
    {
        #region Constants

        const int MAX_PARTICLE_TIME = 120;
        const int DAMAGE_FACTOR = 14;
        const int PARTICLE_SIZE = 6;
        const double PARTICLE_SPEED_OFFSET = 4.0;

        #endregion

        #region ctor

        public Particle()
        {
            Lifetime = MAX_PARTICLE_TIME;
            Size = PARTICLE_SIZE;
            DamageFactor = DAMAGE_FACTOR;
            IsIntelligent = false;
        }

        #endregion

        #region Creator

        public static Particle Create(Ship ship, ParticleUpgrades ups = null)
        {
            var x = ship.X;
            var y = ship.Y;
            var particle = new Particle { Game = ship.Game, Owner = ship };
            var speed = PARTICLE_SPEED_OFFSET + ship.Speed;
            var ta = particle.DegreeToGrad(ship.DegreeAngle);
            particle.Vx = speed * Math.Sin(ta);
            particle.Vy = -speed * Math.Cos(ta);

            if (ups != null)
                ups.Upgrade(particle);

            var radius = particle.Size / 2.0 + ship.Size / 2.0 + 1.0;
            x += radius * Math.Sin(ta);
            y -= radius * Math.Cos(ta);
            particle.X = x;
            particle.Y = y;
            ship.Game.Objects.Add(particle);
            return particle;
        }

        #endregion

        #region Properties

        public double Vx { get; set; }
	    public double Vy { get; set; }
	    public Ship Owner { get; set; }
        public int Lifetime { get; set; }
        public int DamageFactor { get; set; }
        public bool IsIntelligent { get; set; }
        public override int Damage { get { return (DamageFactor * Lifetime) / MAX_PARTICLE_TIME; } set { } }

        #endregion

        #region Methods

        public override void Logic()
        {
            if (IsIntelligent)
                ArtificialIntelligence();

            X += Vx;
            Y += Vy;
            PeriodicBoundaryConditions();
            Lifetime--;

            if (Lifetime == 0)
                Dead();
        }

        private void ArtificialIntelligence()
        {
            if (Game.ActiveShips == (Owner.Respawn ? 0 : 1))
                return;

            var from = Point.CreateFromObject(this);
            var enemy = Owner.ClosestEnemy();

            if (enemy == null)
                return;

            var to = Point.CreateFromObject(enemy);
            var angle = Game.AngleBetween(from, to);
            var speed = Math.Sqrt(Vx * Vx + Vy * Vy);
            var dsp = Math.Sqrt(Math.Pow(Owner.X - X, 2.0) + Math.Pow(Owner.Y - Y, 2.0));
            var radiuses = (Owner.Size + Size) / 2.0;
            
            if (dsp < radiuses + speed)
            {
                var gamma = Game.AngleBetween(from, Point.CreateFromObject(Owner));
                var alpha = Game.AngleBetween(Point.Origin, new Point { X = dsp, Y = radiuses + 1.0 });

                if (angle > gamma - alpha && angle < gamma + alpha)
                {
                    if (angle < gamma)
                        angle = gamma - alpha;
                    else
                        angle = gamma + alpha;
                }
            }

            Vx = speed * Math.Sin(angle);
            Vy = speed * Math.Cos(angle);
        }

        public override JsonObject ToJson()
        {
            var json = new JsonObject();
            json["owner"] = Owner.Id;
            json["x"] = X;
            json["y"] = Y;
            json["size"] = Size;
            json["lifetime"] = Lifetime;
            json["remove"] = Remove;
            return json;
        }

        public void OnCollision(Ship ship)
        {
            if (Game.FriendlyFire || Owner != ship)
            {
                if (ship.Hit(this) && Owner != null)
                {
                    Owner.Points++;
                    InfoText.Add(ship, "+1", 50, this);
                }

                Dead();
            }
        }

        public void OnCollision(Asteroid asteroid)
        {
            if (asteroid.Hit(this) && Owner != null)
                InfoText.Add(asteroid, "Awesome", 50, this);

            Dead();
        }

        public void OnCollision(Bomb bomb)
        {
            if (bomb.Physical)
            {
                Dead();

                if(bomb.Hit(this))
                    InfoText.Add(bomb, "Nice!", 50, this);
            }
        }

        #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