-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
418 lines (347 loc) · 10.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
namespace diep;
using Godot;
using System;
public partial class Player : RigidBody2D
{
//diep stats
[Export] public float HealingSpeed = 0.2f;
[Export] public float Health = 100f;
[Export] public float BodyDamage = 50f;
[Export] public float BulletSpeed = 400f;
[Export] public float BulletDurability = 1.2f;
[Export] public float BulletDamage = 40f;
[Export] public float ReloadSpeed = 5f;
[Export] public float MovementSpeed = 200f;
//target
[Export] public PackedScene TargetScene = (PackedScene)ResourceLoader.Load("res://Target.tscn");
[Export] public int TargetSpawnRange = 350;
private float _targetSpawnTime = 2.2f;
//enemy
[Export] public PackedScene EnemyScene = (PackedScene)ResourceLoader.Load("res://Enemy.tscn");
private Timer _enemySpawnTimer;
private RandomNumberGenerator _rng;
[Export] public int EnemySpawnRange = 600;
//shooting
private bool _autoShootEnabled;
private float _shootTimer;
private PackedScene _bulletScene;
private Timer _spawnTimer;
//xp
private int _level;
private int _upgradePoints;
//collisions
private bool _collisionCooldown;
private Timer _collisionCooldownTimer;
//help files
private LevelManager _levelManager;
private UpgradeManager _upgradeManager;
private HealthManager _healthManager;
private PackedScene _enemyScene;
//health + xpbar
private ProgressBar _healthBar;
private ProgressBar _xpBar;
private float XPBarWidth = DisplayServer.WindowGetSize().X;
private float XPBarHeight = DisplayServer.WindowGetSize().Y *0.03f; //scaling factor
public override void _Ready()
{
GravityScale = 0;
//scenes and files
_bulletScene = ResourceLoader.Load<PackedScene>("res://Bullet.tscn");
_healthManager = new HealthManager(Health, HealingSpeed, 3.5f);
_healthManager.Connect(nameof(HealthManager.PlayerDied), new Callable(this, nameof(OnPlayerDied)));
AddChild(_healthManager);
//audio
var upgradeSoundPlayer = new AudioStreamPlayer();
AddChild(upgradeSoundPlayer);
_levelManager = new LevelManager();
_upgradeManager = new UpgradeManager(_levelManager, upgradeSoundPlayer);
//enemy
_enemyScene = ResourceLoader.Load<PackedScene>("res://Enemy.tscn");
var enemySpawnTimer = new Timer();
enemySpawnTimer.WaitTime = 12.0f;
enemySpawnTimer.OneShot = false;
enemySpawnTimer.Timeout += SpawnEnemiesInBulk;
AddChild(enemySpawnTimer);
enemySpawnTimer.Start();
_rng = new RandomNumberGenerator();
_rng.Randomize();
//shooting
_shootTimer = 0;
_spawnTimer = new Timer();
_spawnTimer.OneShot = true;
AddChild(_spawnTimer);
_spawnTimer.Timeout += SpawnSingleTarget;
ScheduleNextSpawn();
//collisions
ContactMonitor = true;
MaxContactsReported = 5;
_collisionCooldownTimer = new Timer();
_collisionCooldownTimer.WaitTime = 0.2f;
_collisionCooldownTimer.OneShot = true;
AddChild(_collisionCooldownTimer);
_collisionCooldownTimer.Timeout += () => _collisionCooldown = false;
//healthbar
_healthManager._currentHP = Health;
_healthBar = GetNode<ProgressBar>("ProgressBar");
_healthBar.MaxValue = Health;
_healthBar.Value = _healthManager._currentHP;
//xpbar
InitializeXPBar();
}
public override void _Process(double delta)
{
HandleMovement();
HandleShooting((float)delta);
_healthManager.UpdateTimeSinceLastDamage((float)delta);
_healthManager.Heal((float)delta);
_upgradeManager.HandleUpgradeInputs();
_healthBar.Value = _healthManager._currentHP;
//update xpbar size
base._Process(delta);
UpdateXPBarSize();
}
public override void _PhysicsProcess(double delta)
{
AngularVelocity = 0;
Rotation = 0;
RotationDegrees = 0;
foreach (var body in GetCollidingBodies())
{
if (body is Target target)
{
HandlePlayerTargetCollision(target);
}
}
}
private void HandleMovement()
{
Vector2 inputVelocity = GetInput();
LinearVelocity = inputVelocity;
}
private void HandlePlayerTargetCollision(RigidBody2D target)
{
if (_collisionCooldown)
return;
if (target is Enemy enemy)
{
TakeDamage(enemy._enemyDamage);
enemy.TakeDamage(BodyDamage);
}
else if (target is Target targetObject)
{
_healthManager.TakeDamage(targetObject._targetDamage);
target.Call("TakeDamage", BodyDamage);
}
Vector2 collisionDirection = GlobalPosition.DirectionTo(target.GlobalPosition).Normalized();
float playerBounceStrength = 300f;
float targetBounceStrength = 5000f;
ApplyCentralImpulse(collisionDirection * playerBounceStrength);
target.ApplyCentralImpulse(collisionDirection * targetBounceStrength);
Rotation = 0;
AngularVelocity = 0;
_collisionCooldown = true;
_collisionCooldownTimer.Start();
}
private void HandleShooting(double delta)
{
if (Input.IsActionJustPressed("toggle_auto_shoot")) //prees E keyboard
{
_autoShootEnabled = !_autoShootEnabled;
}
_shootTimer += (float)delta;
if (_autoShootEnabled && _shootTimer >= 1.0 / ReloadSpeed)
{
Shoot();
_shootTimer = 0;
}
else if (!_autoShootEnabled && Input.IsActionPressed("shoot") && _shootTimer >= 1.0 / ReloadSpeed)
{
Shoot();
_shootTimer = 0;
}
}
private Vector2 GetInput()
{
Vector2 inputVector = Vector2.Zero;
// handles WASD input as well as arrows
if (Input.IsActionPressed("move_right"))
inputVector.X += 1;
if (Input.IsActionPressed("move_left"))
inputVector.X -= 1;
if (Input.IsActionPressed("move_down"))
inputVector.Y += 1;
if (Input.IsActionPressed("move_up"))
inputVector.Y -= 1;
return inputVector.Normalized() * MovementSpeed;
}
private void Shoot()
{
Vector2 mousePosition = GetGlobalMousePosition();
Vector2 direction = (mousePosition - GlobalPosition).Normalized();
var bullet = _bulletScene.Instantiate<Bullet>();
bullet.GlobalPosition = GlobalPosition;
bullet.Rotation = direction.Angle();
if (bullet is { } bulletScript)
{
bulletScript.Direction = direction;
bulletScript.BulletSpeed = BulletSpeed;
bulletScript.BulletDamage = BulletDamage;
bulletScript.BulletDurability = BulletDurability;
}
GetParent().AddChild(bullet);
}
private void ScheduleNextSpawn()
{
RandomNumberGenerator rng = new RandomNumberGenerator();
rng.Randomize();
float randomInterval = rng.RandfRange(-1.0f, 1.0f);
_spawnTimer.WaitTime = _targetSpawnTime + randomInterval;
_spawnTimer.Start();
}
private void SpawnSingleTarget()
{
RandomNumberGenerator rng = new RandomNumberGenerator();
rng.Randomize();
float minDistance = 75f;
float maxDistance = TargetSpawnRange;
float angle = rng.RandfRange(0, Mathf.Tau);
float distance = rng.RandfRange(minDistance, maxDistance);
Vector2 randomOffset = new Vector2(
Mathf.Cos(angle) * distance,
Mathf.Sin(angle) * distance
);
Vector2 targetPosition = GlobalPosition + randomOffset;
var target = TargetScene.Instantiate<Target>();
target.GlobalPosition = targetPosition;
float initialImpulseX = rng.RandfRange(-50f, 50f);
float initialImpulseY = rng.RandfRange(-50f, 50f);
Vector2 initialImpulse = new Vector2(initialImpulseX, initialImpulseY);
target.SetInitialImpulse(initialImpulse, 10f);
GetParent().CallDeferred("add_child", target);
ScheduleNextSpawn();
}
private void SpawnEnemiesInBulk()
{
RandomNumberGenerator rng = new RandomNumberGenerator();
rng.Randomize();
int enemyCount = rng.RandiRange(2, 4);
float minDistance = 50f;
float maxDistance = TargetSpawnRange;
for (int i = 0; i < enemyCount; i++)
{
float angle = rng.RandfRange(0, Mathf.Tau);
float distance = rng.RandfRange(minDistance, maxDistance);
Vector2 randomOffset = new Vector2(
Mathf.Cos(angle) * distance,
Mathf.Sin(angle) * distance
);
Vector2 spawnPosition = GlobalPosition + randomOffset;
var enemy = _enemyScene.Instantiate<Enemy>();
enemy.GlobalPosition = spawnPosition;
enemy.Initialize(this);
GetParent().CallDeferred("add_child", enemy);
}
ScheduleNextEnemySpawn();
}
private void ScheduleNextEnemySpawn()
{
if (_enemySpawnTimer != null)
{
RandomNumberGenerator rng = new RandomNumberGenerator();
rng.Randomize();
float randomInterval = rng.RandfRange(1.0f, 2.0f);
_enemySpawnTimer.WaitTime = randomInterval;
_enemySpawnTimer.Start();
}
}
private void HealPlayer()
{
_healthManager.Heal(HealingSpeed);
}
private void FreeAllNodes() {
var nodes = GetTree().GetNodesInGroup("all");
foreach (Node n in nodes) {
if (n.IsInsideTree()) {
n.QueueFree();
}
}
}
private void ResetGame()
{
FreeAllNodes();
GetTree().ReloadCurrentScene();
}
public void TakeDamage(float damage)
{
_healthManager._currentHP -= damage;
_healthBar.Value -= Mathf.Clamp(_healthManager._currentHP, 0, Health);
}
private void OnPlayerDied()
{
GD.Print("Player has died!");
QueueFree();
GetTree().Quit();
//return; //is an easy fix of course but lets not make it so dirty-cheap
}
private void InitializeXPBar()
{
_xpBar = new ProgressBar();
_xpBar.Size = new Vector2(XPBarWidth, XPBarHeight);
_xpBar.MaxValue = _levelManager.GetXPForNextLevel();
_xpBar.Value = _levelManager._currentXP;
_xpBar.Position = new Vector2(-XPBarWidth/2,300);
AddChild(_xpBar);
}
public void UpdateXPBar()
{
int currentXPWithinLevel = _levelManager.GetCurrentXPWithinLevel();
int xpRangeForCurrentLevel = _levelManager.GetXPRangeForCurrentLevel();
_xpBar.MaxValue = xpRangeForCurrentLevel;
_xpBar.Value = currentXPWithinLevel;
}
public void AddXP(int xp)
{
_levelManager.AddXP(xp);
UpdateXPBar();
}
private void HandleUpgradeInputs()
{
_upgradeManager.HandleUpgradeInputs();
}
public Vector2 GetPlayerPosition()
{
return GlobalPosition;
}
public void UpdateHealthBar(float newMaxHealth)
{
_healthManager._maxHP = newMaxHealth;
_healthBar.MaxValue = newMaxHealth;
_healthBar.Value = Mathf.Min(_healthManager._currentHP, newMaxHealth);
GD.Print("Health bar updated: Max Health = " + newMaxHealth);
}
public void ResetHealthBar()
{
_healthManager._maxHP = 100;
_healthBar.MaxValue = 100;
_healthBar.Value = Mathf.Min(_healthManager._currentHP, 100);
GD.Print("Health bar reset to original settings.");
}
private void UpdateXPBarSize()
{
Vector2 screenSize = DisplayServer.WindowGetSize();
float xpBarWidth = screenSize.X * 1f;
float xpBarHeight = screenSize.Y * 0.03f;
_xpBar.Size = new Vector2(xpBarWidth, xpBarHeight);
Vector2 xpBarPosition = new Vector2(screenSize.X - 1.5f*xpBarWidth, xpBarHeight+screenSize.Y/2 - 2.4f*xpBarHeight);
_xpBar.Position = xpBarPosition;
//color - no function needed...
var theme = new Theme();
var fgStyle = new StyleBoxFlat();
fgStyle.BgColor = new Color(1.0f, 0.8f, 0.0f);
var bgStyle = new StyleBoxFlat();
bgStyle.BgColor = new Color(0.2f, 0.2f, 0.2f);
theme.SetStylebox("fill", "ProgressBar", fgStyle);
theme.SetStylebox("background", "ProgressBar", bgStyle);
_xpBar.Theme = theme;
}
}