コード例 #1
0
ファイル: PlayerFactory.cs プロジェクト: timgarwood/invaders
        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);
        }
コード例 #2
0
        protected void SpawnProjectile(string projectileName, ProjectileSource source)
        {
            var slop = 20;
            //spawn the projectile just outside the players bounding box in the direction the player is facing.
            var offset       = GameUtils.RotationToVec2((float)(RigidBody.GetAngle() * 180 / System.Math.PI));
            var offsetLength = GameUtils.PhysicsVec(new Vector2(0, (Texture.Height + slop) / 2));

            offset = offset * offsetLength.Length();

            WeaponFactory.Instance.CreateProjectile(projectileName, RigidBody.GetPosition() + offset, RigidBody.GetAngle(), source);
        }
コード例 #3
0
ファイル: AlienFactory.cs プロジェクト: timgarwood/invaders
        /// <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");
        }