Example #1
0
        /**
         *  Update function
         *
         */
        internal void Update(GameTime gameTime)
        {
            //var ticks = DateTime.Now.Ticks;
            this.viewMatrix  = Matrix.CreateLookAt(Camera, Target, Vector3.Up);
            this.worldMatrix = Matrix.CreateWorld(Vector3.Backward, Vector3.Forward, Vector3.Up);
            //this.projectionMatrix = Matrix.CreatePerspective(GraphicsDevice.Viewport.AspectRatio, 1.0f, 0.1f, 100.0f);
            this.projectionMatrix = Matrix.CreateOrthographic(2f * GraphicsDevice.Viewport.AspectRatio, 2f, 0.1f, 100f);


            int screenw = GraphicsDevice.Viewport.Bounds.Width;
            int screenh = GraphicsDevice.Viewport.Bounds.Height;

            if (WorldMap.MapChanged)
            {
                NewMapTexture(screenw, screenh);
            }

            //if (gameTime.TotalGameTime.TotalSeconds - delta > 0.1)
            //{

            //}
            //namedQuads["map"].Position = new Vector2(Camera.X, Camera.Y);

            WorldMap.Player.Location = WorldMap.ConvertLocationToPixelPosition(new Vector2(Camera.X / aspect, Camera.Y));

            sprites["timer"].Position = FixedPixelPositionToVector2(screenw / 2, 32);
            var seconds = (float)gameTime.TotalGameTime.TotalSeconds - prevSeconds;

            if (WorldMap.Update(seconds, gameTime))
            {
                prevSeconds = (float)gameTime.TotalGameTime.TotalSeconds;
                PlaySound("timer");
            }
            if (!WorldMap.Disaster)
            {
                //sprites["timer"].isAnimated = true;
            }

            //if (null != vertexBuffer) { vertexBuffer.Dispose(); vertexBuffer = null;  }
            var quadCount = this.namedQuads.Count;

            foreach (var sprite in sprites)
            {
                quadCount += sprite.Value.Animations.Count;
            }

            //6 for quad, 2 for normal, 2 for texture
            vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), quadCount * (6 + 2 + 2), BufferUsage.WriteOnly);

            //Console.WriteLine("Engine Update time: {0}", DateTime.Now.Ticks - ticks);

            if (WorldMap.Player.Is("hurt"))
            {
                sprites["sfx"].AddAnimation("bloody", FixedPixelPositionToVector2(screenw / 2, screenh / 2), 9998);
                PlaySound("hurt");
                WorldMap.Player.Set("hurt", 0);
            }
            if (WorldMap.Forces.Count > 0)
            {
                foreach (var force in WorldMap.Forces)
                {
                    string sound = String.Empty;
                    switch (force.Sound)
                    {
                    case Force.Sounds.Default:
                        sound = "attack";
                        break;

                    case Force.Sounds.Flame:
                        sound = "flame";
                        break;

                    case Force.Sounds.Beam:
                        //sound = "beam";
                        break;

                    case Force.Sounds.Explosion:
                        sound = "explosion";
                        break;
                    }

                    if (null == attackSound)
                    {
                        attackSound = new Dictionary <Force.Sounds, SoundEffectInstance>();
                    }

                    if (attackSound.ContainsKey(force.Sound))
                    {
                        if (attackSound[force.Sound].State == SoundState.Stopped)
                        {
                            attackSound[force.Sound].Play();
                        }
                    }
                    else
                    {
                        if (!sound.Equals(string.Empty))
                        {
                            attackSound.Add(force.Sound, PlaySound(sound));
                        }
                    }
                }
            }
        }
Example #2
0
        //float newAngle = 0;

        /*
         * Draw
         */
        internal void Draw(GraphicsDevice GraphicsDevice, Microsoft.Xna.Framework.GameTime gameTime)
        {
            //var ticks = DateTime.Now.Ticks;
            int screenw = GraphicsDevice.Viewport.Bounds.Width;
            int screenh = GraphicsDevice.Viewport.Bounds.Height;

            GraphicsDevice.SetVertexBuffer(null);
            List <VertexPositionNormalTexture> verts = new List <VertexPositionNormalTexture>();

            float seconds = (float)gameTime.TotalGameTime.TotalSeconds - prevSeconds;

            if (seconds > 10.0f)
            {
                prevSeconds = (float)gameTime.TotalGameTime.TotalSeconds;
            }
            delta = gameTime.TotalGameTime.TotalSeconds;

            // portals rendered in wrong spots; why?
            foreach (var portal in WorldMap.Portals)
            {
                //Console.WriteLine("Portal: " + portal.ToString());
                //var v = PixelPositionToVector2((int)((portal.Location.X - WorldMap.X)), (int)((portal.Location.Y - WorldMap.Y)));

                /*var v = PixelPositionToVector2(
                 *  (int)((portal.Location.X- (WorldMap.MapWidth/2))),
                 *  (int)((portal.Location.Y- (WorldMap.MapHeight/2)))
                 *  );*/
                var v = this.WorldMap.WorldMapLocationToVector2(portal.Location);
                if (portal.isOpen)
                {
                    sprites["tiles"].AddAnimation("portal", v, portal.ID);
                }
                if (portal.Destroyed)
                {
                    sprites["misc"].AddAnimation("bigportal_destroyed", v, portal.ID);
                }
            }
            sprites["tiles"].PruneUnusedAnimations(WorldMap.Portals.Where((x) => (x.isOpen)).Select((x) => (x.ID)));

            var playerLocation = WorldMap.WorldMapLocationToVector2(WorldMap.Player.Location);
            //playerLocation = new Vector2(loc.X/aspect, loc.Y);
            //renderQuads.AddRange(sprites.Values);
            var player = sprites["player"];

            if (WorldMap.Player.Is("attacking"))
            {
                if (player.Animation == "idle")
                {
                    player.Animations.Clear();
                }
                player.AddAnimation("attack", playerLocation, WorldMap.Player.ID);
            }
            else
            {
                player.Animations.Clear();
                player.AddAnimation("idle", playerLocation, WorldMap.Player.ID);
            }

            //sprites["player"].PruneUnusedAnimations(new int [] {9999});
            //sprites["player"].PruneUnusedAnimations(new int[] { WorldMap.Player.ID });



            var enemies = sprites["enemies"];

            enemies.CameraPosition = Camera;
            //enemies.Current = 0;
            //enemies.SetTileToCurrent(GraphicsDevice);
            sprites["enemies"].PruneUnusedAnimations(WorldMap.Creatures.Select((i) => (i.ID)), true);

            foreach (var enemy in WorldMap.Creatures)
            {
                var enemylocation = WorldMap.WorldMapLocationToVector2(enemy.Location);
                //var enemylocation = PixelPositionToVector2((int)(enemy.Location.X - WorldMap.X), (int)(enemy.Location.Y - WorldMap.Y));
                if (enemy.Is("hurt"))
                {
                    sprites["sfx"].AddAnimation("bloody", enemylocation, enemy.ID);
                    enemy.Set("hurt", enemy.Get("hurt") - 0.1f);
                }

                string type = string.Empty;
                switch (enemy.Type)
                {
                case Creature.Types.PLAYER:
                    // do nothing as player is rendered separately.
                    break;

                case Creature.Types.SMALL:
                    type = "small";
                    break;

                case Creature.Types.MEDIUM:
                    type = "medium";
                    break;

                case Creature.Types.LARGE:
                    type = "large";
                    break;

                case Creature.Types.BEWARE:
                    type = "itcomes";
                    break;

                case Creature.Types.BOSS:
                    type = "boss";
                    break;

                default:

                    break;
                }
                if (!type.Equals(string.Empty))
                {
                    bool reset = false;
                    if (enemy.Is("dead"))
                    {
                        type = string.Format("{0}_dead", type);
                        enemy.Set("dead", enemy.Get("dead") - 0.1f);
                        reset = true;
                        enemies.AddAnimation(type, enemylocation, enemy.ID, reset);
                    }
                    else
                    {
                        enemies.AddAnimation(type, enemylocation, enemy.ID, reset);
                    }
                }
            }


            foreach (var loc in WorldMap.Locations)
            {
                if (WorldMap.EndGame && loc.Type == "EndGame")
                {
                    var v    = WorldMap.WorldMapLocationToVector2(new Vector2(loc.X, loc.Y));
                    var misc = sprites["misc"];
                    misc.AddAnimation("bigportal", v, 1234);
                }
            }
            //sprites["misc"].PruneUnusedAnimations(WorldMap.Locations.Select((i) => (i.ID)));

            var sfx = sprites["sfx"];

            //Console.WriteLine("sfx count: {0}, creature count: {1}", WorldMap.Forces.Count, WorldMap.Creatures.Count);
            foreach (var force in WorldMap.Forces)
            {
                //var v = PixelPositionToVector2((int)(force.Location.X), (int)(force.Location.Y));
                var v = WorldMap.WorldMapLocationToVector2(force.Location);
                sfx.Delay = 0.250f;
                string type = string.Empty;
                switch (force.Visual)
                {
                case Force.Visuals.Test:
                    type = "row1";
                    break;

                case Force.Visuals.Beam:
                    type = "beam";
                    break;

                case Force.Visuals.Test2:
                    type = "row2";
                    break;

                case Force.Visuals.Bloody:
                    type = "bloody";
                    v    = FixedPixelPositionToVector2(screenw / 2, screenh / 2);
                    break;

                case Force.Visuals.Explosion:
                    type = "explosion";
                    //v = TenGame.AdjustVector2(WorldMap.Viewport, TenGame.screenw / 2, TenGame.screenh / 2);
                    break;

                default:
                    sfx.Animation = string.Empty;
                    break;
                }

                var sfxanim = sfx.AddAnimation(type, v, force.ID);
                var atk     = force as Attack;

                if (null != atk)
                {
                    sfxanim.Angle = atk.Direction;
                }

                if (force.IsApplied)
                {
                    if (!sfxanim.Playing)
                    {
                        force.Remove = true;
                    }
                }
            }
            sprites["sfx"].PruneUnusedAnimations(WorldMap.Forces.Select((i) => (i.ID)));



            sprites["cards"].ClearAnimations();
            int xshift = 0;

            foreach (var card in WorldMap.Player.Cards)
            {
                string type = string.Empty;
                switch (card.Type)
                {
                case Card.Types.Bomb:
                    type = "bomb";
                    break;

                case Card.Types.Heal:
                    type = "heal";
                    break;

                case Card.Types.Sign:
                    type = "sign";
                    break;
                }
                if (type != string.Empty)
                {
                    sprites["cards"].AddAnimation(type, FixedPixelPositionToVector2((64 * xshift) + 64, screenh - 67), xshift);
                }
                xshift++;
            }


            verts.AddRange(namedQuads["map"].Vertices);

            sprites["cards"].AddAnimation("sign", new Vector2(0.0f, 0.0f));

            sprites["cards"].AddAnimation("heal", new Vector2(0.2f, 0.0f));


            sprites["cards"].AddAnimation("bomb", FixedPixelPositionToVector2(screenw - 64, screenh - 64));

            foreach (var sprite in sprites.Values)
            {
                foreach (var anim in sprite.Animations)
                {
                    var tile = sprite.GetPositionedTile(sprite.Tiles[anim.getNextAllowedIndex((float)gameTime.TotalGameTime.TotalSeconds)], anim.Position, Camera.Z);
                    verts.AddRange(tile);
                }
            }

            if (null != vertexBuffer)
            {
                if (vertexBuffer.VertexCount < verts.Count)
                {
                    //overflow
                    Console.WriteLine("Vertexcount:" + verts.Count);
                    vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), verts.Count * 10, BufferUsage.WriteOnly);
                }
                else
                {
                    vertexBuffer.SetData(verts.ToArray());
                }
            }

            //vertexBuffer.SetData<VertexPositionNormalTexture>(0, verts.ToArray(), 0, verts.Count, 32);

            GraphicsDevice.BlendState = BlendState.AlphaBlend;

            GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            GraphicsDevice.SetVertexBuffer(vertexBuffer);

            SetupEffect();
            // foreach object in some list
            if (sprites["timer"].Current == 41)
            {
                this.WorldMap.Disaster = true;
                seconds = 10;
                //sprites["timer"].isAnimated = false;
            }

            foreach (var quad in namedQuads.Values)
            {
                if (quad.Show)
                {
                    RenderVertices(GraphicsDevice, quad.Texture, quad.Vertices);
                }
            }

            foreach (var kvpair in sprites)
            {
                var sheet = kvpair.Value;
                //Console.WriteLine("Animations for {0}: {1}", kvpair.Key, sheet.Animations.Count);
                foreach (var anim in sheet.Animations)
                {
                    RenderVertices(GraphicsDevice, sheet.Sheet,
                                   sheet.GetPositionedTile(sheet.Tiles[anim.CurrentFrame], anim.Position, anim.Scale, anim.Angle, Camera.Z),
                                   anim.Opacity);
                }
            }

            //Console.WriteLine("Engine Draw time: {0}", DateTime.Now.Ticks - ticks);
        }