Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.96 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. using Penumbra;
  10. using Console = System.Console;
  11. using RoyT.AStar;
  12. using System.Threading;
  13.  
  14. namespace CoT
  15. {
  16.     public class Player : Creature
  17.     {
  18.  
  19.         float speed = 200f;
  20.         Vector2 direction, nextPosition, targetPos;
  21.         Position[] path;
  22.         Position nextTileInPath;
  23.         bool normalMoving, pathMoving;
  24.         FloatRectangle bottomHitBox;
  25.  
  26.         enum HeroClass
  27.         {
  28.  
  29.         }
  30.  
  31.         private Penumbra.Light light;
  32.  
  33.         public Player(string texture, Vector2 position, Rectangle sourceRectangle, Vector2 depthSortingOffset, Grid grid, Map map, int hp, int attack, int defense) : base(texture, position, sourceRectangle, depthSortingOffset, map, hp, attack, defense)
  34.         {
  35.             //this.enemies = enemies;
  36.             this.map = map;
  37.             this.grid = grid;
  38.             attackSize = 150;
  39.             light = new PointLight();
  40.             light.Scale = new Vector2(5000, 5000).ToCartesian();
  41.             light.Intensity = 0.2f;
  42.             light.ShadowType = ShadowType.Solid;
  43.             GameManager.Instance.Penumbra.Lights.Add(light);
  44.             Scale = 3;
  45.             LayerDepth = 1f;
  46.  
  47.             CenterMass = new Vector2(PositionOfFeet.X, Position.Y - SourceRectangle.Height * Scale);
  48.             destinationRectangle.Width = (int)(ResourceManager.Get<Texture2D>(Texture).Width * Scale);
  49.             destinationRectangle.Height = (int)(ResourceManager.Get<Texture2D>(Texture).Height * Scale);
  50.             bottomHitBox = new FloatRectangle(new Vector2(Position.X, Position.Y + (int)(SourceRectangle.Height * 0.90 * Scale)),
  51.                 new Vector2(SourceRectangle.Width * Scale, (SourceRectangle.Height * Scale) / 10));
  52.             Offset = new Vector2((float)SourceRectangle.Width / 2, (float)SourceRectangle.Height / 2);
  53.         }
  54.         public override void Update()
  55.         {
  56.             Hitbox = new FloatRectangle(Position, new Vector2(SourceRectangle.Width * Scale, SourceRectangle.Height * Scale));
  57.             base.Update();
  58.             if (Health <= 0)
  59.             {
  60.                 return;
  61.             }
  62.             light.Position = PositionOfFeet;
  63.             Camera.Focus = PositionOfFeet;
  64.  
  65.             if (Input.IsRightClickPressed && !attacking) //Vid musklick får spelaren en ny måldestination och börjar röra sig,
  66.                 //spelaren kan inte röra sig under tiden det tar att utföra en attack
  67.             {
  68.                 targetPos = Input.CurrentMousePosition.ScreenToWorld();
  69.                 direction = GetDirection(PositionOfFeet, targetPos);
  70.                 normalMoving = true;
  71.                 pathMoving = false;
  72.             }
  73.  
  74.             if (normalMoving)
  75.             {
  76.                 CheckForCollision();
  77.             }
  78.  
  79.             if (Vector2.Distance(PositionOfFeet, targetPos) < map.TileSize.Y / 16) //Spelaren slutar röra sig inom 10 pixlar av sin destination
  80.             {
  81.                 normalMoving = false;
  82.                 pathMoving = false;
  83.             }
  84.  
  85.             if (normalMoving)
  86.             {
  87.                 Move(direction);
  88.             }
  89.  
  90.             if (pathMoving)
  91.             {
  92.                 path = Pathing(targetPos);
  93.                 if (path.Length > 1)
  94.                 {
  95.                     nextTileInPath = path[1];
  96.                     PathMove();
  97.                 }
  98.                 else
  99.                 {
  100.                     pathMoving = false;
  101.                     normalMoving = true;
  102.                     direction = GetDirection(PositionOfFeet, targetPos);
  103.                 }
  104.                
  105.             }
  106.             AttackLockTimer();
  107.             InputAttack();
  108.             UpdateVariables();
  109.         }
  110.  
  111.         public void InputAttack() //Vid högerklick attackerar spelaren
  112.         {
  113.             Vector2 attackDirection;
  114.  
  115.             if (Input.IsLeftClickPressed && !attacking)
  116.             {
  117.                 attacking = true;
  118.                 normalMoving = false;
  119.                 pathMoving = false;
  120.                 attackDirection = GetDirection(CenterMass, Input.CurrentMousePosition.ScreenToWorld());
  121.                 DecideEnemiesInRange(attackDirection);
  122.  
  123.                 for (int i = 0; i < 20; i++)
  124.                 {
  125.                     ParticleManager.Instance.Particles.Add(new Particle("lightMask", Position,
  126.                         new Rectangle(0, 0, ResourceManager.Get<Texture2D>("lightMask").Width, ResourceManager.Get<Texture2D>("lightMask").Height),
  127.                         attackDirection + Helper.RandomDirection() / 3, 1000f, 5f, Color.Green, 0f, 0.2f));
  128.                 }
  129.  
  130.                 Camera.ScreenShake(0.1f, 2);
  131.             }
  132.         }
  133.  
  134.         public void AttackLockTimer() //Låser spelaren i en attack under 30 frames
  135.         {
  136.             int attackDuration = 30;
  137.  
  138.             if (attacking)
  139.             {
  140.                 attackTimer++;
  141.                 if (attackTimer >= attackDuration)
  142.                 {
  143.                     attackTimer = 0;
  144.                     attacking = false;
  145.                 }
  146.             }
  147.         }
  148.  
  149.         public void DecideEnemiesInRange(Vector2 direction) //Ser ifall fiendernas mittpunkt är inom 45-grader av den ursprungliga attackvinkeln och inom attackrange
  150.         {
  151.             foreach (Creature c in CreatureManager.Instance.Creatures)
  152.             {
  153.                 if (c is Enemy e)
  154.                 {
  155.                     if (Vector2.Distance(CenterMass, e.CenterMass) <= attackSize)
  156.                     {
  157.                         Vector2 directionToEnemy = GetDirection(CenterMass, e.CenterMass);
  158.                         double angleBetweenEnemyAndAngleToAttack = Math.Acos(Vector2.Dot(direction, directionToEnemy));
  159.  
  160.                         Console.WriteLine(MathHelper.ToDegrees((float)angleBetweenEnemyAndAngleToAttack));
  161.                         float attackCone = MathHelper.ToDegrees((float)(Math.PI / 5));//attackkonen är en kon med 45 graders vinkel
  162.  
  163.                         if (MathHelper.ToDegrees((float)angleBetweenEnemyAndAngleToAttack) < attackCone)
  164.                         {
  165.                             Console.WriteLine("Hit!");
  166.                             e.GetHit(this);
  167.                         }
  168.                         else
  169.                         {
  170.                             Console.WriteLine("Miss!");
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.  
  177.         public void UpdateVariables() //Samlar uppdatering av variabler
  178.         {
  179.             float bottomHitBoxWidth = SourceRectangle.Width * Scale / 5;
  180.             bottomHitBox = new FloatRectangle(new Vector2(Position.X + ((float)SourceRectangle.Width * Scale / 2) - ((float)bottomHitBoxWidth / 2),
  181.                 Position.Y + (int)(SourceRectangle.Height * 0.90 * Scale)), new Vector2(bottomHitBoxWidth, (SourceRectangle.Height * Scale) / 10));
  182.             CenterMass = new Vector2(PositionOfFeet.X, PositionOfFeet.Y - (SourceRectangle.Height / 2) * Scale);
  183.             destinationRectangle.X = (int)Position.X;
  184.             destinationRectangle.Y = (int)Position.Y;
  185.             Position = new Vector2(PositionOfFeet.X - (ResourceManager.Get<Texture2D>(Texture).Width * Scale) / 2,
  186.                 PositionOfFeet.Y - (ResourceManager.Get<Texture2D>(Texture).Height * Scale));
  187.         }
  188.  
  189.         public void Move(Vector2 direction) //Förflyttar spelaren med en en riktningsvektor, hastighet och deltatid
  190.         {
  191.             PositionOfFeet += direction * speed * Time.DeltaTime;
  192.         }
  193.  
  194.         public void CheckForCollision() //Kollision med väggtile-check
  195.         {
  196.             int stoppingDistance = map.TileSize.Y / 16;
  197.             for (int x = 0; x < map.TileMap.GetLength(0); x++)
  198.             {
  199.                 for (int y = 0; y < map.TileMap.GetLength(1); y++)
  200.                 {
  201.                     Vector2 tilePos = map.GetTilePosition(new Vector2(x, y)).ToCartesian();
  202.                     Vector2 estimatedHitboxPos = (PositionOfFeet + (direction * stoppingDistance)).ToCartesian();
  203.                     //Vector2 hitboxPos = bottomHitBox.Position.ToCartesian();
  204.                     FloatRectangle hitbox = new FloatRectangle(estimatedHitboxPos, bottomHitBox.Size);
  205.  
  206.                     if (hitbox.Intersects(new FloatRectangle(tilePos, new Vector2(80, 80))) && map.TileMap[x, y].TileType == TileType.Collision)
  207.                     {
  208.                         normalMoving = false;
  209.                         pathMoving = true;
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.  
  215.         public void PathMove() //Rörelse via Pathfinding
  216.         {
  217.             //if (pathMoving)
  218.             {
  219.                 nextPosition = new Vector2(nextTileInPath.X * map.TileSize.Y, nextTileInPath.Y * map.TileSize.Y).ToIsometric();
  220.                 nextPosition.X += map.TileSize.X / 2;
  221.                 nextPosition.Y += map.TileSize.Y / 2;
  222.                 direction.X = nextPosition.X - PositionOfFeet.X;
  223.                 direction.Y = nextPosition.Y - PositionOfFeet.Y;
  224.                 direction.Normalize();
  225.                 PositionOfFeet += direction * speed * Time.DeltaTime;
  226.                 Position = new Vector2(PositionOfFeet.X - (ResourceManager.Get<Texture2D>(Texture).Width * Scale) / 2, PositionOfFeet.Y - (ResourceManager.Get<Texture2D>(Texture).Height * Scale));
  227.             }    
  228.         }
  229.  
  230.         public Vector2 GetDirection(Vector2 currentPos, Vector2 targetPos) //Ger en normaliserad riktning mellan två positioner
  231.         {
  232.             Vector2 targetDirection = targetPos - currentPos;
  233.  
  234.             targetDirection.Normalize();
  235.  
  236.             return targetDirection;
  237.         }
  238.  
  239.         public override void Draw(SpriteBatch sb)
  240.         {
  241.             //Debug
  242.             //FullHitbox
  243.             sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)Hitbox.Position.X, (int)Hitbox.Position.Y, (int)Hitbox.Size.X, (int)Hitbox.Size.Y), Color.Red * 0.1f);
  244.            
  245.             //BottomHitox
  246.             sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)bottomHitBox.Position.X, (int)bottomHitBox.Position.Y, (int)bottomHitBox.Size.X, (int)bottomHitBox.Size.Y), Color.Red * 0.5f);
  247.  
  248.             //CenterMass
  249.             sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)CenterMass.X, (int)CenterMass.Y, (int)bottomHitBox.Size.X, (int)bottomHitBox.Size.Y), Color.Black * 0.9f);
  250.  
  251.             //foreach (Creature c in CreatureManager.Instance.Creatures) invulnerability
  252.             //{
  253.             //    //Enemy CenterMass
  254.             //    sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)e.CenterMass.X, (int)e.CenterMass.Y, (int)bottomHitBox.Size.X, (int)bottomHitBox.Size.Y), Color.Black * 0.9f);
  255.             //}
  256.  
  257.  
  258.  
  259.             //for (int i = 0; i < GameStateManager.Instance.Map.TileMap.GetLength(0); i++)
  260.             //{
  261.             //    for (int j = 0; j < GameStateManager.Instance.Map.TileMap.GetLength(1); j++)
  262.             //    {
  263.             //        Tile t = GameStateManager.Instance.Map.TileMap[i, j];
  264.  
  265.             //        Vector2 pos = GameStateManager.Instance.Map.GetTilePosition(new Vector2(i, j)).ToCartesian();
  266.  
  267.             //        if (t.TileType == TileType.Wall)
  268.             //            sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)pos.X, (int)pos.Y, map.TileSize.Y, map.TileSize.Y), Color.Purple * 0.3f);
  269.             //    }
  270.             //}
  271.             //Vector2 hitboxPos = bottomHitBox.Position.ToCartesian();
  272.             //FloatRectangle hitbox = new FloatRectangle(hitboxPos, bottomHitBox.Size);
  273.             //sb.Draw(ResourceManager.Get<Texture2D>("rectangle"), new Rectangle((int)hitbox.Position.X, (int)hitbox.Position.Y, (int)bottomHitBox.Size.X, (int)bottomHitBox.Size.Y), Color.Red * 0.5f);
  274.  
  275.  
  276.             base.Draw(sb);
  277.  
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement