-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyPositionGenerator.cs
48 lines (41 loc) · 1.39 KB
/
EnemyPositionGenerator.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
using System;
using Microsoft.Xna.Framework;
namespace RPG
{
class EnemyPositionGenerator
{
private Vector2 _canvasSize;
private float _enemyRadius;
private Random _random;
public EnemyPositionGenerator(Vector2 canvasSize, float enemyRadius)
{
_canvasSize = canvasSize;
_enemyRadius = enemyRadius;
_random = new Random();
}
public Vector2 Generate()
{
switch (RandomOrigin())
{
case Origin.Top:
return new(RandomX(), -_enemyRadius);
case Origin.Bottom:
return new(RandomX(), _canvasSize.Y + _enemyRadius);
case Origin.Left:
return new(-_enemyRadius, RandomY());
case Origin.Right:
return new(_canvasSize.X + _enemyRadius, RandomY());
default:
throw new RpgException("Never gonna happen!");
}
}
private Origin RandomOrigin()
{
Array values = Enum.GetValues(typeof(Origin));
return (Origin)values.GetValue(Random.Shared.Next(values.Length));
}
private int RandomX() => _random.Next((int)_canvasSize.X);
private int RandomY() => _random.Next((int)_canvasSize.Y);
private enum Origin { Top, Bottom, Left, Right }
}
}