-
Notifications
You must be signed in to change notification settings - Fork 0
/
Racket.cs
110 lines (86 loc) · 3.05 KB
/
Racket.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
using FNAEngine2D;
using FNAEngine2D.Collisions;
using FNAEngine2D.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pong
{
/// <summary>
/// Objet représentant la raquette
/// </summary>
public class Racket: GameObject, IUpdate
{
/// <summary>
/// Position à partir du bas de l'écran pour la raquette
/// </summary>
private const int BOTTOM_POSITION = -100;
/// <summary>
/// Vitesse de déplacement
/// </summary>
private float _speedPixelsPerSeconds = 400f;
/// <summary>
/// Raquette
/// </summary>
private TextureBox _racket;
/// <summary>
/// Taille de la raquette
/// </summary>
private Point _size = new Point(100, 20);
/// <summary>
/// Type to manage the colliders
/// </summary>
private Type[] _colliderTypes = new Type[] { typeof(GameBorder) };
/// <summary>
/// Moment du dernier fire
/// </summary>
private float _lastTimeFireSecond = 99f;
/// <summary>
/// Firerate
/// </summary>
private float _fireRatePerSeconds = 0.5f;
/// <summary>
/// Chargement du contenu
/// </summary>
protected override void Load()
{
this.Bounds = new Rectangle((this.Game.Width / 2) - (_size.X / 2), this.Game.Height + BOTTOM_POSITION, _size.X, _size.Y);
_racket = Add(new TextureBox("racket", this.Bounds));
this.EnableCollider();
}
/// <summary>
/// Exécution à chaque frame
/// </summary>
public void Update()
{
//Déplacement de la balle...
if (Input.IsKeyDown(Keys.Left) || Input.IsKeyDown(Keys.A))
this.TranslateX(-_speedPixelsPerSeconds * this.ElapsedGameTimeSeconds);
if (Input.IsKeyDown(Keys.Right) || Input.IsKeyDown(Keys.D))
this.TranslateX(_speedPixelsPerSeconds * this.ElapsedGameTimeSeconds);
Collision collision = this.GetCollision(this.X, this.Y, _colliderTypes);
if (collision != null)
{
//On retourne d'où on vient...
this.TranslateTo(collision.StopLocation);
}
_lastTimeFireSecond += this.ElapsedGameTimeSeconds;
//Bullets?
if (Input.IsKeyDown(Keys.Space))
{
if (_lastTimeFireSecond >= _fireRatePerSeconds)
{
_lastTimeFireSecond = 0;
PongGame.Instance.Add(new Bullet(this.X, this.Y - Bullet.BULLET_HEIGHT));
PongGame.Instance.Add(new Bullet(this.Right, this.Y - Bullet.BULLET_HEIGHT));
GetContent<SoundEffect>("sfx\\fire").Data.Play();
}
}
}
}
}