-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
80 lines (67 loc) · 2.04 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
using System;
using SFML.System;
using SFML.Graphics;
using System.Collections.Generic;
namespace EquationInvasion
{
public class Player
{
private List<RectangleShape> _bullets;
private CircleShape _shape;
private const float BULLET_HEIGHT = 20.0f;
private const float BULLET_SPEED = 20.0f;
public Player (FloatRect screenRect)
{
// setup player
_shape = new CircleShape(30, 3);
_shape.Position = new Vector2f(screenRect.Width/2 - (_shape.Radius), screenRect.Height - (_shape.Radius*2));
_shape.FillColor = Color.Green;
_bullets = new List<RectangleShape> ();
}
public void Draw(RenderWindow window)
{
window.Draw (_shape);
// Draw the bullets on the screen and move then up
// Remove them once they leave the screen
foreach (RectangleShape b in _bullets.ToArray()) {
window.Draw (b);
if (b.Position.Y > 0 + BULLET_HEIGHT)
b.Position = new Vector2f (b.Position.X, b.Position.Y - BULLET_SPEED);
else {
_bullets.Remove (b);
break;
}
}
}
public void Move(Direction d, FloatRect screenRect)
{
float dist = 10.5f;
// Move players position only if its inside the screen
switch (d) {
case Direction.LEFT:
if (_shape.Position.X >= dist)
_shape.Position = new Vector2f (_shape.Position.X - dist, _shape.Position.Y);
break;
case Direction.RIGHT:
if (_shape.Position.X <= screenRect.Width - (_shape.Radius*2) - dist)
_shape.Position = new Vector2f (_shape.Position.X + dist, _shape.Position.Y);
break;
}
}
public void Shoot()
{
RectangleShape bullet = new RectangleShape (new Vector2f(10, BULLET_HEIGHT));
bullet.Position = new Vector2f(_shape.Position.X + (_shape.Radius/2) + (bullet.Size.X), _shape.Position.Y - (bullet.Size.Y/2));
bullet.FillColor = Color.Green;
_bullets.Add (bullet);
}
public List<RectangleShape> bullets
{
get { return _bullets; }
}
public void RemoveBullet (RectangleShape b)
{
_bullets.Remove (b);
}
}
}