Advertisement
Wolvenspud

enemy

Oct 18th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9.  
  10. namespace GameTemplate
  11. {
  12.     public class Entity : GameObject
  13.     {
  14.         protected float Speed = 100;
  15.         protected Vector2 Velocity;
  16.  
  17.         protected float Health = 100;
  18.  
  19.         protected bool WallDown = false;
  20.         protected bool WallUp = false;
  21.         protected bool WallLeft = false;
  22.         protected bool WallRight = false;
  23.  
  24.         public Entity(Vector2 StartPosition, Texture2D SetSprite) : base(StartPosition, SetSprite)
  25.         {
  26.             Velocity = Vector2.Zero;
  27.         }
  28.  
  29.         public void HitWall(GameObject Other)//assuming evrything is a square
  30.         {
  31.             Vector2 WallDirection = Other.GetPosition() - Position;
  32.             WallDirection.Normalize();
  33.  
  34.             if (Math.Abs(WallDirection.X) > Math.Abs(WallDirection.Y))//hit wall left or right
  35.             {
  36.                
  37.                 if(WallDirection.X < 0)//left
  38.                 {
  39.                     WallLeft = true;
  40.                     Position.X = Other.GetPosition().X + LevelManager.TileSize.X;//push back out of wall
  41.                 }
  42.                 else//right
  43.                 {
  44.                     WallRight = true;
  45.                     Position.X = Other.GetPosition().X - LevelManager.TileSize.X;//push back out of wall
  46.                 }
  47.             }
  48.             else//hit wall up or down
  49.             {
  50.                 if (WallDirection.Y < 0)//Up
  51.                 {
  52.                     WallUp = true;
  53.                     Position.Y = Other.GetPosition().Y + LevelManager.TileSize.Y;//push back out of wall
  54.                 }
  55.                 else//Down
  56.                 {
  57.                     WallDown = true;
  58.                     Position.Y = Other.GetPosition().Y - LevelManager.TileSize.Y;//push back out of wall
  59.                 }
  60.             }
  61.         }
  62.  
  63.         public override void Update(float DT)
  64.         {
  65.             Position += Velocity * DT;
  66.             base.Update(DT);
  67.  
  68.  
  69.             //reset wall touch
  70.             WallDown = false;
  71.             WallUp = false;
  72.             WallLeft = false;
  73.             WallRight = false;
  74.     }
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement