/
Key.cs~RF4f1d88e8.TMP
87 lines (77 loc) · 2.87 KB
/
Key.cs~RF4f1d88e8.TMP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
namespace GameBehaviour
{
public class Key : RigidBody2D
{
public RigidBody2D ObjRB;
public bool heldByDrone = false;
public bool heldByPlayer = false;
Texture2D Texture;
BoxCollider boxCollider;
PolygonCollider polygonCollider;
SpriteBatch SpriteBatch;
public Key(RigidBody2D rb, Texture2D tex, SpriteBatch spriteBatch, Vector2 position, Vector2 rotation, float scale, string tag, bool isStatic, float friction) :
base(rb.Position, rb.Rotation, rb.Scale, rb.Tag, rb.IsStatic, rb.Friction)
{
Texture = tex;
SpriteBatch = spriteBatch;
ObjRB = rb;
ObjRB.Mass = 4;
boxCollider = new BoxCollider(Position, new Vector2(Position.X + Texture.Width, Position.Y + Texture.Height), Texture.Width, Texture.Height);
boxColl = boxCollider;
polygonCollider = new PolygonCollider();
polygonColl = polygonCollider;
SetPolygonPoints(polygonCollider);
}
public override void OnCollision(Manifold man)
{
RigidBody2D collisionObj = man.A == (RigidBody2D)this ? man.B : man.A;//check which object we are
if (collisionObj.Tag == "player")
{
heldByPlayer = true;
}
if (collisionObj.Tag == "drone")
{
heldByDrone = true;
}
base.OnCollision(man);
}
void SetPolygonPoints(PolygonCollider p)
{
p.points.Clear();
//top left
p.points.Add(new Vector2(Position.X, Position.Y));
//top right
p.points.Add(new Vector2(Position.X + Texture.Width, Position.Y));
//bottom right
p.points.Add(new Vector2(Position.X + Texture.Width, Position.Y + Texture.Height));
//bottom left
p.points.Add(new Vector2(Position.X, Position.Y + Texture.Height));
//create it
p.BuildEdges();
//create it
p.BuildEdges();
}
public override void Draw(SpriteBatch spr)
{
SpriteBatch.Draw(Texture, Position, Color.White);
}
public override void Update(GameTime gameTime)
{
//Position = ObjRB.Position;
boxCollider.topLeft = new Vector2(Position.X, Position.Y);
boxCollider.bottomRight = new Vector2(Position.X + Texture.Width, Position.Y + Texture.Height);
boxColl = boxCollider;
polygonColl = polygonCollider;
SetPolygonPoints(polygonColl);
}
}
}