/
Player.cs
203 lines (164 loc) · 7.05 KB
/
Player.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics;
namespace gameProject
{
public class Player:GameModel
{
//Movement
float m_Rotation = 0, m_TurnAmount = 3.0f;
Vector2 m_Direction = Vector2.Zero
, m_OrginalPos = Vector2.Zero;
Vector2 m_Velocity
{
get { return RigidBody.LinearVelocity; }
set { RigidBody.LinearVelocity = value; }
}
public HealthBar HealthBar;
//Wind
enum WindState { NoWind, BuildUp, BuildOff }
WindState m_WindState = WindState.NoWind;
float m_WindTime = 0,
m_WindTimePause = 5,
m_FinalWindForce = 0,
m_CurrentWindForce = 0,
m_Multplier = 1f,
m_CurrentIncrement = 0.05f;
Vector2 m_WindDirection = Vector2.Zero;
bool m_bInput = true;
public Player(string assetFile, RenderContext context, Vector2 originalPos)
: base(assetFile, "Textures/Collision/c_Piece1", context)
{
base.Name = "Player";
m_OrginalPos = originalPos;
//Create rigidBody
RigidBody = BodyFactory.CreateRectangle(context.World, ConvertUnits.ToSimUnits(20), ConvertUnits.ToSimUnits(10), 1.0f);
RigidBody.BodyType = BodyType.Dynamic;
RigidBody.OnCollision += RigidBody_OnCollision;
RigidBody.OnSeparation +=RigidBody_OnSeparation;
RigidBody.UserData = this; //Add this class as data for easy access in collision
RigidBody.Restitution = 0.1f;
Depth = -200;
//Xml
var xmlPlayerData = XmlLoader.Load<PlayerSettings>("PlayerSettings");
m_Multplier = xmlPlayerData.multiplier;
m_TurnAmount = xmlPlayerData.turnAmount;
m_CurrentIncrement = xmlPlayerData.windIncrement;
m_Velocity = Vector2.Zero;
}
private void RigidBody_OnSeparation(Fixture fixtureA, Fixture fixtureB)
{
}
bool RigidBody_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
HealthBar.RemoveHealth();
return true;
}
public void Update(RenderContext context)
{
float deltaTime = (float)context.GameTime.ElapsedGameTime.Milliseconds / 100;
HandleInput();
if (m_bInput)
{
RandomWind(deltaTime);
Movement(deltaTime);
}
Rotate(0, 0, m_Rotation);
base.Update();
}
void Movement(float deltaTime)
{
var windVec = Wind();
windVec -= RigidBody.LinearVelocity;
var dot = windVec.X * (float)Math.Cos(RigidBody.Rotation + Math.PI / 2);
var sin = (float)Math.Sin(RigidBody.Rotation + Math.PI / 2);
dot += windVec.Y * (float)Math.Sin(RigidBody.Rotation + Math.PI / 2);
m_Velocity += m_Multplier * new Vector2(dot * (float)Math.Cos(RigidBody.Rotation + Math.PI / 2), dot * (float)Math.Sin(RigidBody.Rotation + Math.PI / 2));
}
void HandleInput()
{
var keyState = Keyboard.GetState();
var padState = GamePad.GetState(PlayerIndex.One,GamePadDeadZone.Circular);
// See if GamePad is connected and use that
if (padState.IsConnected && (padState.ThumbSticks.Right.Y != 0f && padState.ThumbSticks.Right.X != 0f))
{
m_Rotation = (float)Math.Atan2(padState.ThumbSticks.Right.Y, padState.ThumbSticks.Right.X);
m_bInput = true;
}
else
{
if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
{
m_Rotation += m_TurnAmount;
m_bInput = true;
}
else if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D))
{
m_Rotation -= m_TurnAmount;
m_bInput = true;
}
}
}
void RandomWind(float delta)
{
var xmlPlayerData = XmlLoader.Load<PlayerSettings>("PlayerSettings");
switch (m_WindState)
{
case WindState.NoWind:
m_WindTime += delta;
if (m_WindTime >= m_WindTimePause)
{
m_WindTime = 0f;
Random rnd = new Random();
if (xmlPlayerData.timeBetweenWindBursts == -1) m_WindTimePause = rnd.Next(1, 15);
else m_WindTimePause = xmlPlayerData.timeBetweenWindBursts;
m_WindState = WindState.BuildUp; //initialise wind burst
m_FinalWindForce = (float)(rnd.NextDouble() * 10.0);
m_WindDirection.X = (float)(rnd.Next(2)) - 1;
m_WindDirection.Y = 0f;
}
break;
case WindState.BuildUp:
if (m_CurrentWindForce < m_FinalWindForce)
{
m_CurrentWindForce += m_CurrentIncrement;
// m_Rotation += m_TurnAmount;
}
else
m_WindState = WindState.BuildOff;
break;
case WindState.BuildOff:
if (m_CurrentWindForce > 0.1f) //0.1 so there's always some wind
m_CurrentWindForce -= m_CurrentIncrement;
else
{
m_CurrentWindForce = 0.1f;
m_WindState = WindState.NoWind;
}
break;
}
}
Vector2 Wind()
{
Vector2 wind = new Vector2( m_Multplier * m_FinalWindForce * m_WindDirection.X, 2f * (Position.Y / (m_OrginalPos.Y / 2))); //was -5
var xmlPlayerData = XmlLoader.Load<PlayerSettings>("PlayerSettings");
if (xmlPlayerData.windX != -1f) wind = new Vector2(xmlPlayerData.windX, wind.Y);
if (xmlPlayerData.windY != -1f) wind = new Vector2(wind.X, xmlPlayerData.windY);
return wind;
}
public void DisplaySettings(RenderContext context)
{
TextRenderer.DrawText("Mulitplier: " + m_Multplier +
"\nTurn Amount: " + m_TurnAmount +
"\nIncrement: " + m_CurrentIncrement +
"\nVelocity: " + m_Velocity +
"\nWindDirection: " + m_WindDirection
,context.ViewPortSize.X * 0.75f, context.ViewPortSize.Y * 0.05f, Color.White, context);
}
}
};