Esempio n. 1
0
		void ICmpRenderer.Draw(IDrawDevice device)
		{
			// Create a buffer to cache and re-use vertices. Not required, but will boost performance.
			if (this.buffer == null) this.buffer = new CanvasBuffer();

			// Create a Canvas to auto-generate vertices from high-level drawing commands.
			Canvas canvas = new Canvas(device, this.buffer);
			canvas.State.TextFont = this.font;

			// If the game is over or won, display "game over" screen
			if (this.gameOver || this.gameWin)
			{
				// Various animation timing variables.
				float animOffset = this.gameWin ? 0.0f : 2500.0f;
				float animTime = this.gameWin ? 10000.0f : 4500.0f;
				float blendDurationRatio = this.gameWin ? 0.6f : 0.5f;
				float textOffsetRatio = this.gameWin ? 0.2f : 0.0f;

				float timeSinceGameOver = (float)Time.MainTimer.TotalMilliseconds - this.lastTimeAnyAlive;
				float gameOverAnimProgress = MathF.Clamp((timeSinceGameOver - animOffset) / animTime, 0.0f, 1.0f);
				float controlInfoAnimProgress = MathF.Clamp(((timeSinceGameOver - animOffset) - animTime - 2000.0f) / 2000.0f, 0.0f, 1.0f);
				float blendAnimProgress = MathF.Clamp(gameOverAnimProgress / blendDurationRatio, 0.0f, 1.0f);
				float textAnimProgress = MathF.Clamp((gameOverAnimProgress - blendDurationRatio - textOffsetRatio) / (1.0f - blendDurationRatio - textOffsetRatio), 0.0f, 1.0f);

				if (this.blendMaterial != null && blendAnimProgress > 0.0f)
				{
					canvas.PushState();

					if (this.gameOver)
					{
						// Set up our special blending Material and specify the threshold to blend to
						this.blendMaterial.SetUniform("threshold", 1.0f - blendAnimProgress);
						canvas.State.SetMaterial(this.blendMaterial);
						canvas.State.ColorTint = ColorRgba.Black;

						// Specify a texture coordinate rect so it spans the entire screen repeating itself, instead of being stretched
						if (this.blendMaterial.MainTexture != null)
						{
							Random rnd = new Random((int)this.lastTimeAnyAlive);
							Vector2 randomTranslate = rnd.NextVector2(0.0f, 0.0f, canvas.State.TextureBaseSize.X, canvas.State.TextureBaseSize.Y);
							canvas.State.TextureCoordinateRect = new Rect(
								randomTranslate.X, 
								randomTranslate.Y, 
								device.TargetSize.X / canvas.State.TextureBaseSize.X, 
								device.TargetSize.Y / canvas.State.TextureBaseSize.Y);
						}
					}
					else
					{
						// If we won, simply fade to white
						canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Add, ColorRgba.White));
						canvas.State.ColorTint = ColorRgba.White.WithAlpha(blendAnimProgress);
					}

					// Fill the screen with a rect of our Material
					canvas.FillRect(0, 0, device.TargetSize.X, device.TargetSize.Y);

					canvas.PopState();
				}

				if (this.font != null && textAnimProgress > 0.0f)
				{
					canvas.PushState();

					// Determine which text to draw to screen and where to draw it
					string gameOverText = this.gameWin ? "is it over..?" : "darkness...";
					Vector2 fullTextSize = canvas.MeasureText(gameOverText);
					Vector2 textPos = device.TargetSize * 0.5f - fullTextSize * 0.5f;
					gameOverText = gameOverText.Substring(0, MathF.RoundToInt(gameOverText.Length * textAnimProgress));

					// Make sure not to draw inbetween pixels, so the text is perfectly sharp.
					textPos.X = MathF.Round(textPos.X);
					textPos.Y = MathF.Round(textPos.Y);

					// Draw the text to screen
					canvas.State.ColorTint = this.gameWin ? ColorRgba.Black : ColorRgba.White;
					canvas.DrawText(gameOverText, textPos.X, textPos.Y);

					canvas.PopState();
				}

				if (controlInfoAnimProgress > 0.0f)
				{
					Vector2 infoBasePos = device.TargetSize * 0.5f + new Vector2(0.0f, device.TargetSize.Y * 0.25f);
					if (this.controlInfoMouseKeyboard != null)
					{
						canvas.PushState();

						Vector2 texSize = this.controlInfoMouseKeyboard.Res.MainTexture.Res.Size * 0.5f;

						canvas.State.SetMaterial(this.controlInfoMouseKeyboard);
						canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress);
						canvas.FillRect(
							infoBasePos.X - texSize.X * 0.5f,
							infoBasePos.Y - texSize.Y - 10,
							texSize.X,
							texSize.Y);

						canvas.PopState();
					}
					if (this.controlInfoGamepad != null)
					{
						canvas.PushState();

						Vector2 texSize = this.controlInfoGamepad.Res.MainTexture.Res.Size * 0.5f;

						canvas.State.SetMaterial(this.controlInfoGamepad);
						canvas.State.ColorTint = ColorRgba.White.WithAlpha(controlInfoAnimProgress);
						canvas.FillRect(
							infoBasePos.X - texSize.X * 0.5f,
							infoBasePos.Y + 10,
							texSize.X,
							texSize.Y);

						canvas.PopState();
					}
				}
			}
		}
Esempio n. 2
0
        /// <summary>
        /// Load stuff here
        /// </summary>
        protected override void Initialize()
        {
            var device	=	GraphicsDevice;

            base.Initialize();

            vb			=	new VertexBuffer(device,  typeof(Vertex), 6 );
            cb			=	new ConstantBuffer(GraphicsDevice, typeof(ConstData) );
            instDataGpu	=	new StructuredBuffer( device, typeof(InstData), InstanceCount, StructuredBufferFlags.None );
            instDataCpu	=	new InstData[ InstanceCount ];

            var rand = new Random();
            for (int i=0; i<InstanceCount; i++) {
                instDataCpu[ i ].Offset		=	rand.NextVector2( new Vector2(-2.5f,-2f), new Vector2( 2.5f,2f) );
                instDataCpu[ i ].Scale		=	rand.NextFloat( 0, 0.7f);
                instDataCpu[ i ].Rotation	=	rand.NextFloat( 0, MathUtil.TwoPi );
                instDataCpu[ i ].Color		=	rand.NextVector4( Vector4.Zero, Vector4.One * 0.7f );
                instDataCpu[ i ].TexId		=	rand.Next(4);
            }

            Reloading += InstancingDemo_Reloading;

            InstancingDemo_Reloading ( this, EventArgs.Empty );
        }
Esempio n. 3
0
        public static void Update()
        {
            isUpdating = true;

            foreach (Entity entity in Entities)
                entity.PreUpdate();

            //bvhTree.Recalculate();
            IEnumerable<CollisionData> Collisions = GetProbableCollisions();

            foreach (CollisionData data in Collisions)
            {
                bool collision = data.Check();
                if (collision)
                {
                    Random rand = new Random();
                    data.CalculateN();
                    data.CalculateJ();

                    ((PhysicsEntity)data.Object1).Collision(data);
                    data.Swap();
                    ((PhysicsEntity)data.Object1).Collision(data);

                    float hue1 = 2;
                    float hue2 = (hue1 + rand.NextFloat(0, 2)) % 6f;
                    Color4 color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
                    Color4 color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);

                    for (int i = 0; i < 150; i++)
                    {
                        float speed = 12.0f * (1.0f - 1 / rand.NextFloat(1, 3));

                        Color4 color = Color4.Lerp(color1, color2, rand.NextFloat(0, 1));
                        Vector2 dir = rand.NextVector2(1, 1);
                        ParticleManager.CreateParticle((data.Point1 + data.Point2) / 2, color, 190, Vector2.One, dir * speed, 0.2f);
                    }
                }
            }

            foreach (Entity entity in Entities)
                entity.Update();

            foreach (Entity entity in Entities)
                entity.PostUpdate();

            isUpdating = false;

            for (int i = Entities.Count - 1; i >= 0; i--)
            {
                Entity ent = Entities[i];
                if (ent.isExpired)
                {
                    Disposer.RemoveAndDispose(ref ent);
                    Entities.RemoveAt(i);
                }
            }

            Entities.AddRange(addedEntities);

            addedEntities.Clear();
        }
Esempio n. 4
0
		/// <summary>
		/// Load stuff here
		/// </summary>
		protected override void Initialize ()
		{
			var device	=	GraphicsDevice;

			base.Initialize();

			vb			=	new VertexBuffer(device,  typeof(Vertex),   6 );
			vbi			=	new VertexBuffer(device,  typeof(Instance), InstanceCount );
			cb			=	new ConstantBuffer(GraphicsDevice, typeof(ConstData) );

			instDataCpu	=	new Instance[ InstanceCount ];

			var rand = new Random();
			for (int i=0; i<InstanceCount; i++) {
				
				var offset		=	rand.NextVector2( new Vector2(-2.5f,-2f), new Vector2( 2.5f,2f) );
				var scale		=	rand.NextFloat( 0, 0.7f);
				var rotation	=	rand.NextFloat( 0, MathUtil.TwoPi );

				instDataCpu[ i ].InstanceTransform		=	new Vector4( offset, scale, rotation );
				instDataCpu[ i ].InstanceColor			=	rand.NextVector4( Vector4.Zero, Vector4.One * 0.7f );
				instDataCpu[ i ].InstanceTextureId		=	rand.Next(4);
			}

			//	handle hot reload :
			Reloading += InstancingDemo2_Reloading;
			InstancingDemo2_Reloading( this, EventArgs.Empty );
		}
Esempio n. 5
0
        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {

                mouse = Mouse.GetState();
                // bloom
                bloom.Visible = true;
                bloom.Settings = BloomSettings.PresetSettings[bloomSettingsIndex];

                // update entites
                playerBody.update(gameTime);
                Grid.Update();
                ParticleManager.Update();

               Grid.ApplyExplosiveForce(5f, playerBody.playerBody.Position * 64, 80);
               foreach (playerLaser laser in playerBody.getLasers)
               {
               //   Grid.ApplyExplosiveForce(5f, laser.laserBody.Position * 64, 50);
                  // Grid.ApplyImplosiveForce(5f, laser.laserBody.Position * 64, 50);
                  Grid.ApplyExplosiveForce(5f, laser.laserBody.Position * 64, 50);

               }

                if (playerBody.isAlive == false)
                {
                    playerBody.isAlive = true;
                }

                foreach (SeekerDrone drone in Drones)
                {
                    drone.setTarget(playerBody.playerBody.Position * 64);

                    drone.update(gameTime);

                    if (drone.getIsDead)
                    {
                        //  if(!cam2D.getShake)
                        cam2D.Shake(300f, 1000f);
                        Grid.ApplyExplosiveForce(20f, drone.getBody.Position * 64, 1000);

                        Random randP = new Random();
                        float hue1 = randP.NextFloat(0, 6);
                        float hue2 = (hue1 + randP.NextFloat(0, 2)) % 6f;
                        Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
                        Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);

                        for (int i = 0; i < 120; i++)
                        {
                            float speed = 18f * (1f - 1 / randP.NextFloat(1f, 10f));
                            var state = new ParticleState()
                            {
                                Velocity = randP.NextVector2(speed, speed),
                                Type = ParticleType.Enemy,
                                LengthMultiplier = 1f
                            };
                            Color color = Color.Lerp(color1, color2, randP.NextFloat(0, 1));
                            ParticleManager.CreateParticle(particleArt, drone.getBody.Position * 64f, color, 190, new Vector2(1.5f), state);
                        }

                        world.RemoveBody(drone.getBody);

                        playerBody.updateScore(2); /// update this to a getter and setter!!!
                    }
                }
                for (int k = 0; k < Drones.Count; ++k)
                {
                    if (Drones[k].getIsDead)
                    {
                        Drones.RemoveAt(k);
                    }
                }
                //game script

                foreach (Pickupable pickupable in Pickupables)
                {

                    if (pickupable.getSetIsTouchingPlayer && playerBody.getSetWantsToPickUp && !playerBody.getSetHasPickedUp)
                    {
                      // joints.Add ( JointFactory.CreateRevoluteJoint(world,playerBody.playerBody,
                        //   pickupable.getBody, new Vector2(0,0)));

                        Random randP = new Random();
                        float hue1 = randP.NextFloat(0, 6);
                        float hue2 = (hue1 + randP.NextFloat(0, 2)) % 6f;
                        Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
                        Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);

                        for (int i = 0; i < 30; i++)
                        {
                            float speed = 18f * (1f - 1 / randP.NextFloat(1f, 10f));
                            var state = new ParticleState()
                            {
                                Velocity = randP.NextVector2(speed, speed),
                                Type = ParticleType.Enemy,
                                LengthMultiplier = 0.4f
                            };
                            Color color = Color.Lerp(color1, color2, randP.NextFloat(0, 1));
                            ParticleManager.CreateParticle(particleArt, pickupable.getBody.Position * 64f, color, 190, new Vector2(1.5f), state);
                        }
                       joints.Add( JointFactory.CreateDistanceJoint(world, playerBody.playerBody, pickupable.getBody, new Vector2(0, 0), new Vector2(0, 0)));

                        playerBody.getSetWantsToPickUp = false;
                        pickupable.getSetIsAttachedToPlayer = true;

                        playerBody.getSetHasPickedUp = true;

                    }

                    if (joints.Count > 0 && playerBody.getSetWantsTodrop && playerBody.getSetHasPickedUp && pickupable.getSetIsAttachedToPlayer )
                    {
                        // for some reason the on seperation dosn't work when removing a joint.
                        world.RemoveJoint(joints[0]);
                        joints.RemoveAt(0);
                        playerBody.getSetHasPickedUp = false;
                        pickupable.getSetIsAttachedToPlayer = false;
                        pickupable.getSetIsTouchingPlayer = false;

                    }

                }

                // limts on the cam.

                cam2D.MaxRotation = 0.001f;
                cam2D.MinRotation = -0.001f;

                cam2D.MaxPosition = new Vector2(((playerBody.playerBody.Position.X) * 64 + 1), ((playerBody.playerBody.Position.Y) * 64) + 1);
                cam2D.MinPosition = new Vector2(((playerBody.playerBody.Position.X) * 64) + 2, ((playerBody.playerBody.Position.Y) * 64) + 1);
                cam2D.Update(gameTime);

                world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
            }
        }