Esempio n. 1
0
        public Player CreatePlayer(Texture2D crateTexture)
        {
            var crateShapeDef    = new PolygonDef();
            var cratePhysicsSize = GameUtils.PhysicsVec(new Vector2(crateTexture.Width, crateTexture.Height));

            crateShapeDef.Vertices    = new Vec2[4];
            crateShapeDef.Vertices[0] = new Vec2(-(cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2));
            crateShapeDef.Vertices[1] = new Vec2((cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2));
            crateShapeDef.Vertices[2] = new Vec2((cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2));
            crateShapeDef.Vertices[3] = new Vec2(-(cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2));
            crateShapeDef.VertexCount = 4;

            Logger.Info($"crate size = ({cratePhysicsSize.X},{cratePhysicsSize.Y})");
            crateShapeDef.Density             = GameData.PlayerDensity;
            crateShapeDef.Friction            = GameData.PlayerFriction;
            crateShapeDef.Filter.CategoryBits = CollisionCategory.Player;
            crateShapeDef.Filter.MaskBits     = (ushort)(CollisionCategory.Wall | CollisionCategory.Alien | CollisionCategory.AlienProjectile | CollisionCategory.Pickup);

            var crateBodyDef = new BodyDef();

            crateBodyDef.IsBullet = true;
            var playerPosition = new Vec2(GameData.PlayerStartX, GameData.PlayerStartY);

            crateBodyDef.Position.Set(playerPosition.X, playerPosition.Y);
            var crateBody  = PhysicsWorld.CreateBody(crateBodyDef);
            var crateShape = crateBody.CreateShape(crateShapeDef);

            crateBody.SetMassFromShapes();

            var player = new Player(Content, PhysicsWorld, crateTexture, GameWorld, crateShape, crateBody, AnimationFactory, WeaponInventory, FilteredInputListener, GameData, GameUtils);

            GameWorld.AddGameObject(player);
            return(player);
        }
Esempio n. 2
0
        public override void OnCollision(GameObject other, Vec2 position)
        {
            if (!PendingDispose)
            {
                if (other is Projectile)
                {
                    var proj = other as Projectile;
                    if (!proj.PendingDispose)
                    {
                        GameWorld.AddGameObject(AnimationFactory.Create(position, "LaserExplosion"));

                        Player.TotalScore++;

                        Hp -= proj.Definition.Damage;
                        if (Hp <= 0)
                        {
                            Player.TotalScore += _definition.ScoreValue;
                            PendingDispose     = true;
                            DeathLocation      = RigidBody.GetPosition();
                            DeathEffect.Play();
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// creates an instance of the given alien definition name
        /// </summary>
        /// <param name="name"></param>
        public Alien Create(string name)
        {
            if (_alienDefinitions != null)
            {
                var definition = _alienDefinitions.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                if (definition == null)
                {
                    throw new Exception($"No Alien definition found for name {name}");
                }

                var rand        = new Random((int)(DateTime.UtcNow - DateTime.MinValue).Ticks);
                var texture     = _contentManager.Load <Texture2D>(definition.TextureName);
                var physicsSize = GameUtils.PhysicsVec(new Vector2(texture.Width * definition.Scale, texture.Height * definition.Scale));
                var minX        = GameData.MaxXDimension * .1;
                var minY        = GameData.MaxYDimension * .1;
                var maxX        = GameData.MaxXDimension - minX;
                var maxY        = GameData.MaxYDimension - minY;

                var loc = new Vec2(rand.Next((int)minX, (int)maxX), rand.Next((int)minY, (int)maxY));

                var shapeDef = new PolygonDef();
                shapeDef.Vertices = new Vec2[definition.Vertices.Length];
                for (var i = 0; i < definition.Vertices.Length; ++i)
                {
                    var x = definition.Vertices[i].X * definition.Scale;
                    var y = definition.Vertices[i].Y * definition.Scale;
                    shapeDef.Vertices[i] = GameUtils.PhysicsVec(new Vector2(x, y));
                }

                shapeDef.VertexCount = shapeDef.Vertices.Length;

                shapeDef.Density             = definition.Density;
                shapeDef.Friction            = definition.Friction;
                shapeDef.Filter.CategoryBits = CollisionCategory.Alien;
                shapeDef.Filter.MaskBits     = (ushort)(CollisionCategory.Wall | CollisionCategory.Player | CollisionCategory.PlayerProjectile | CollisionCategory.Alien);

                var bodyDef = new BodyDef();
                bodyDef.IsBullet = true;
                bodyDef.Position.Set(loc.X, loc.Y);
                var body  = _physicsWorld.CreateBody(bodyDef);
                var shape = body.CreateShape(shapeDef);

                body.SetMassFromShapes();

                var healthBar = HealthBarFactory.Create("AlienHealthBar", 100, 5);

                var gameObject = new Alien(_contentManager, _physicsWorld, GameData, GameUtils, definition, _animationFactory, GameWorld, texture, shape, body, Player, healthBar);
                GameWorld.AddGameObject(gameObject);
                return(gameObject);
            }

            throw new Exception("Uninitialized definitions");
        }
Esempio n. 4
0
        public override void OnCollision(GameObject other, Vec2 position)
        {
            if (other is Projectile)
            {
                var proj = other as Projectile;
                if (!proj.PendingDispose)
                {
                    GameWorld.AddGameObject(AnimationFactory.Create(position, "LaserExplosion"));
                    Hp -= proj.Definition.Damage;
                    if (Hp <= 0)
                    {
                        Hp = 0;

                        LivesRemaining--;
                        if (LivesRemaining < 0)
                        {
                            LivesRemaining = 0;
                        }

                        if (Active)
                        {
                            DeathEffect.Play();
                            AnimationFactory.Create(RigidBody.GetPosition(), "AlienExplosion");
                        }

                        Active = false;
                    }
                }
            }
            else if (other is Health)
            {
                var health = other as Health;
                Hp += health.Hp;
                if (Hp >= MaxHp)
                {
                    Hp = MaxHp;
                }

                HealthEffect.Play();
            }
            else if (other is Laser)
            {
                WeaponEffect.Play();

                var laser = other as Laser;
                WeaponInventory.AddToInventory(laser);
            }
        }