public override void Update(GameTime gameTime, TeeEngine engine) { Hero player = (Hero)engine.GetEntity("Player"); KeyboardState keyboardState = Keyboard.GetState(); if (KeyboardExtensions.GetKeyDownState(keyboardState, Keys.S, this, true) && Entity.IntersectsWith(this, null, player, "Shadow", gameTime)) { if (CurrentDrawableState != "Open") { Random random = new Random(); CurrentDrawableState = "Open"; Drawables.ResetState("Open", gameTime); for (int i = 0; i < 10; i++) { Coin coin = new Coin(this.Pos.X, this.Pos.Y, 100, (CoinType)random.Next(3)); coin.Pos.X += (float)((random.NextDouble() - 0.5) * 100); coin.Pos.Y += (float)((random.NextDouble() - 0.5) * 100); engine.AddEntity(coin); } } } }
void MapEntrance_MapZoneHit(MapZone sender, Entity entity, TeeEngine engine, GameTime gameTime) { if (KeyboardExtensions.GetKeyDownState(Keyboard.GetState(), ACTIVATE_KEY, engine, true) && entity == engine.GetEntity("Player")) { MapEventArgs mapArgs = new MapEventArgs(); mapArgs.SetProperty("Target", Target); engine.ClearEntities(); engine.LoadMap(Destination, mapArgs); } }
public virtual void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs) { if (mapEventArgs.HasProperty("Target")) { Hero player = new Hero(); MapEntrance targetEntrance = (MapEntrance)engine.GetEntity(mapEventArgs.GetProperty("Target")); player.Pos = targetEntrance.Pos + new Vector2(targetEntrance.Width, targetEntrance.Height) / 2; engine.AddEntity("Player", player); } }
public override void Update(GameTime gameTime, TeeEngine engine) { if (IsOnScreen) { float COIN_MOVE_SPEED = 5000; float TERMINAL_VELOCITY = 5; Hero player = (Hero)engine.GetEntity("Player"); // Find the distance between the player and this coin. float distanceSquared = Vector2.DistanceSquared(Pos, player.Pos); float speed = COIN_MOVE_SPEED / distanceSquared; // Mangitude of velocity. speed = Math.Min(speed, TERMINAL_VELOCITY); if (speed > 0.5) { // Calculate the angle between the player and the coin. double angle = Math.Atan2( player.Pos.Y - this.Pos.Y, player.Pos.X - this.Pos.X ); this.Pos.X += (float)(Math.Cos(angle) * speed); // x component. this.Pos.Y += (float)(Math.Sin(angle) * speed); // y component. // Check to see if coin can be considered collected. if (Entity.IntersectsWith(this, "Shadow", player, "Shadow", gameTime)) { // CoinSound.Play(0.05f, 0.0f, 0.0f); player.Coins += this.CoinValue; engine.RemoveEntity(this); } } } base.Update(gameTime, engine); }
public override void Update(GameTime gameTime, TeeEngine engine) { // Get the Hero player for interaction purposes. Hero player = (Hero)engine.GetEntity("Player"); Vector2 prevPos = Pos; // Check if this Bat has died. if (HP <= 0) { this.Opacity -= 0.02f; this.Drawables.ResetState(CurrentDrawableState, gameTime); if (this.Opacity < 0) { engine.RemoveEntity(this); } } else { // ATTACKING LOGIC. if (_attackStance == AttackStance.Attacking) { this.Pos.X -= (float)(Math.Cos(_attackAngle) * _attackSpeed); this.Pos.Y -= (float)(Math.Sin(_attackAngle) * _attackSpeed); this._attackHeight.Y += 30.0f / ATTACK_COUNTER_LIMIT; this.Drawables.SetGroupProperty("Body", "Offset", _attackHeight); if (Entity.IntersectsWith(this, "Shadow", player, "Shadow", gameTime)) { player.HP -= 3; } if (_attackCounter++ == ATTACK_COUNTER_LIMIT) { _attackStance = AttackStance.NotAttacking; } } // ATTACK PREPERATION LOGIC. else if (_attackStance == AttackStance.Preparing) { _attackHeight.Y -= 2; if (_attackHeight.Y < -40) { _attackHeight.Y = -40; _attackAngle = Math.Atan2( this.Pos.Y - player.Pos.Y, this.Pos.X - player.Pos.X ); _attackStance = AttackStance.Attacking; _attackCounter = 0; } Drawables.SetGroupProperty("Body", "Offset", _attackHeight); } // NON-ATTACKING LOGIC. PATROL AND APPROACH. else if (_attackStance == AttackStance.NotAttacking) { double distance = Vector2.Distance(player.Pos, this.Pos); if (distance < AGRO_DISTANCE) { // Move towards the player for an attack move. double angle = Math.Atan2( player.Pos.Y - this.Pos.Y, player.Pos.X - this.Pos.X ); // Approach Function. double moveValue; if (distance < ATTACK_DISTANCE) { _attackStance = AttackStance.Preparing; moveValue = 0; } else { moveValue = _moveSpeed; } Pos.X += (float)(Math.Cos(angle) * moveValue); Pos.Y += (float)(Math.Sin(angle) * moveValue); } else { // Perform a standard patrol action. Pos.X += (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds - _randomModifier * 90) * 2); } } // Determine the animation based on the change in position. if (Math.Abs(prevPos.X - Pos.X) > Math.Abs(prevPos.Y - Pos.Y)) { if (prevPos.X < Pos.X) { this.CurrentDrawableState = "Right"; } if (prevPos.X > Pos.X) { this.CurrentDrawableState = "Left"; } } else { if (prevPos.Y < Pos.Y) { this.CurrentDrawableState = "Down"; } if (prevPos.Y > Pos.Y) { this.CurrentDrawableState = "Up"; } } } }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Viewport = new Viewport { X = 0, Y = 0, Width = WINDOW_WIDTH, Height = WINDOW_HEIGHT, MinDepth = 0, MaxDepth = 1 }; GraphicsDevice.Clear(Color.Black); TextCounter = 0; float textHeight = DefaultSpriteFont.MeasureString("d").Y; Rectangle pxDestRectangle = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // Draw the World View Port, Centered on the CurrentPlayer Actor. ViewPortInfo viewPort = Engine.DrawWorldViewPort( SpriteBatch, Engine.GetEntity("Player").Pos, Zoom, pxDestRectangle, Color.White, CurrentSampler, DefaultSpriteFont); // DRAW DEBUGGING INFORMATION SpriteBatch.Begin(); { if (showDebugInfo) { // DRAW THE LIGHT MAP OUTPUT TO THE SCREEN FOR DEBUGGING if (LightShader.Enabled) { int lightMapHeight = 100; int lightMapWidth = (int)Math.Ceiling(100 * ((float)LightShader.LightMap.Width / LightShader.LightMap.Height)); SpriteBatch.Draw( LightShader.LightMap, new Rectangle( WINDOW_WIDTH - lightMapWidth, 0, lightMapWidth, lightMapHeight ), Color.White ); } double fps = 1000 / gameTime.ElapsedGameTime.TotalMilliseconds; Color fpsColor = (Math.Ceiling(fps) < 60) ? Color.Red : Color.White; float TX = CurrentPlayer.Pos.X / Engine.Map.TileWidth; float TY = CurrentPlayer.Pos.Y / Engine.Map.TileHeight; this.IsMouseVisible = true; MouseState mouseState = Mouse.GetState(); Vector2 worldCoord = viewPort.GetWorldCoordinates(new Point(mouseState.X, mouseState.Y)); Vector2 tileCoord = worldCoord / (new Vector2(Engine.Map.TileWidth, Engine.Map.TileHeight));; tileCoord.X = (int)Math.Floor(tileCoord.X); tileCoord.Y = (int)Math.Floor(tileCoord.Y); SpriteBatch.DrawString(DefaultSpriteFont, CurrentPlayer.Pos.ToString(), GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, TX.ToString("0.0") + "," + TY.ToString("0.0"), GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Coins=" + CurrentPlayer.Coins, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "HP=" + CurrentPlayer.HP, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, fps.ToString("0.0 FPS"), GeneratePos(textHeight), fpsColor); SpriteBatch.DrawString(DefaultSpriteFont, "Resolution=" + Engine.PixelWidth + "x" + Engine.PixelHeight, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "MapSize=" + Engine.Map.txWidth + "x" + Engine.Map.txHeight, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Sampler=" + CurrentSampler.ToString(), GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Entities On Screen = " + Engine.EntitiesOnScreen.Count, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Total Entities = " + Engine.Entities.Count, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Actual Zoom = " + viewPort.ActualZoom, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Mouse World Coordinates = " + worldCoord, GeneratePos(textHeight), Color.White); SpriteBatch.DrawString(DefaultSpriteFont, "Mouse Tile Coordinates = " + tileCoord, GeneratePos(textHeight), Color.White); } if (showDiagnostics) { StringBuilder builder = new StringBuilder(); builder.AppendLine(Engine.OverallPerformance.Description); builder.AppendLine(Engine.OverallPerformance.ShowAll(false)); builder.AppendLine(Engine.EntityUpdatePerformance.Description); builder.AppendLine(Engine.EntityUpdatePerformance.ShowTop(5)); string textOutput = builder.ToString(); SpriteBatch.DrawString(DefaultSpriteFont, textOutput, new Vector2(0, WINDOW_HEIGHT - DefaultSpriteFont.MeasureString(textOutput).Y), Color.White); } } SpriteBatch.End(); base.Draw(gameTime); }