-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
151 lines (122 loc) · 3.99 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
using System;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;
namespace Game
{
public class Player : Creature
{
// resources
private static readonly Sound StepSound = new Sound(new SoundBuffer("Data/Sounds/step.ogg"));
// constants
private const float StepSoundDelay = 400f;
private const float ShotDelay = 100f;
private const float GunOffsetX = 11f;
private const float GunOffsetY = 42f;
// working vars
private float stepTimer;
private float shotTimer;
// properties
public static Vector2f StartPos { get { return new Vector2f(GameWorld.MapSize / 2f, GameWorld.MapSize / 2f); } }
public int Score { get; set; }
static Player()
{
StepSound.Volume = 20f;
}
public Player(CreatureType creatureType, Vector2f position)
: base(creatureType, position)
{
}
public void Update(float deltaTime, InputState inputState, GameWorld gameWorld)
{
if (Dead)
return;
bool moving = false;
float speed = CreatureType.MovementSpeed;
var prevPosition = new Vector2f(Position.X, Position.Y);
if (inputState.IsKeyPressed[(int)Keyboard.Key.A])
{
Position.X -= deltaTime * speed;
moving = true;
}
if (inputState.IsKeyPressed[(int)Keyboard.Key.D])
{
Position.X += deltaTime * speed;
moving = true;
}
if (inputState.IsKeyPressed[(int)Keyboard.Key.W])
{
Position.Y -= deltaTime * speed;
moving = true;
}
if (inputState.IsKeyPressed[(int)Keyboard.Key.S])
{
Position.Y += deltaTime * speed;
moving = true;
}
bool collides = false;
const float CollisionRadiusSq = (2f * CollisionRadius) * (2f * CollisionRadius);
foreach (var enemy in gameWorld.Enemies)
{
float distanceSq = (enemy.Position.X - Position.X) * (enemy.Position.X - Position.X) +
(enemy.Position.Y - Position.Y) * (enemy.Position.Y - Position.Y);
if (distanceSq < CollisionRadiusSq)
{
collides = true;
break;
}
}
if (Position.X < 0f
|| Position.X > GameWorld.MapSize
|| Position.Y < 0f
|| Position.Y > GameWorld.MapSize)
{
collides = true;
}
if (collides)
Position = prevPosition;
if (moving)
{
stepTimer += deltaTime;
if (stepTimer > StepSoundDelay)
{
stepTimer = 0f;
StepSound.Play();
}
}
else
stepTimer = StepSoundDelay - 50f;
float newRotationRad = (float)Math.Atan2(inputState.MousePositionFromCenter.Y, inputState.MousePositionFromCenter.X);
float newRotation = newRotationRad * 180f / (float)Math.PI;
Rotation = newRotation + 90f;
shotTimer += deltaTime;
if (inputState.IsLMBPressed && shotTimer > ShotDelay)
{
shotTimer = 0f;
// calculating gun offset relative to the world space
float gunOffsetX2 = (float)Math.Cos(newRotationRad + Math.PI / 2f) * GunOffsetX;
float gunOffsetY2 = (float)Math.Sin(newRotationRad + Math.PI / 2f) * GunOffsetX;
gunOffsetX2 += (float)Math.Cos(newRotationRad) * GunOffsetY;
gunOffsetY2 += (float)Math.Sin(newRotationRad) * GunOffsetY;
var bulletPos = new Vector2f(Position.X + gunOffsetX2, Position.Y + gunOffsetY2);
gameWorld.AddShot(bulletPos, newRotation);
}
}
public void MeleeAttacked()
{
if (!Dead )
{
Health -= MeleeAttackDamage;
if (Health < 0)
Health = 0;
MeleeAttackSound.Play();
}
}
public void Resurrect()
{
Score = 0;
Health = CreatureType.MaxHealth;
Position = StartPos;
}
}
}