-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.cs
191 lines (153 loc) · 6.54 KB
/
Field.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ExtremePong
{
public class Field
{
private int _score;
private Texture2D _texture;
private Ball _ball;
public Ball _ball2;
private Paddle _paddle1;
private Paddle _paddle2;
private Point _resolution;
private FadeShader f_ball;
private FadeShader f_paddle1;
private FadeShader f_paddle2;
private RndColorShader r_test;
private SpriteFont f_score;
private SpriteFont f_posX;
private SpriteFont f_posY;
int _score1;
int _score2;
Song song;
public Field(ContentManager content, Point windowSize)
{
Texture2D ball = content.Load<Texture2D>("ball");
Texture2D paddle = content.Load<Texture2D>("paddle");
f_posX = content.Load<SpriteFont>("Pos");
f_posY = content.Load<SpriteFont>("Pos");
f_score = content.Load<SpriteFont>("score");
song = content.Load<Song>("stay_retro");
_resolution = windowSize;
MediaPlayer.Play(song);
MediaPlayer.IsRepeating = true;
MediaPlayer.MediaStateChanged += MediaPlayer_MediaStateChanged;
_ball = new Ball(ball);
//_ball2 = new Ball(ball);
_paddle1 = new Paddle(paddle);
_paddle2 = new Paddle(paddle);
// Shaders on objects
f_ball = new FadeShader(_ball, .085, 0f, .05f);
f_paddle1 = new FadeShader(_paddle1, .085, 0f, .09f);
// Shaders test
r_test = new RndColorShader(_ball, 1.00);
_score1 = 0;
_score2 = 0;
_paddle1.Position = new Vector2(0, (_resolution.Y - _paddle1.Texture.Height) / 2);
_paddle2.Position = new Vector2(_resolution.X - _paddle2.Texture.Width, (_resolution.Y - _paddle2.Texture.Height) / 2);
_ball.Position = new Vector2(((_resolution.X - _ball.Texture.Width) / 2), ((_resolution.Y - _ball.Texture.Height) / 2));
// Starting direction for ball
_ball.Direction = new Vector2(1, 0);
_paddle1.input = new Input() { Up = Keys.W, Down = Keys.S };
_paddle2.input = new Input() { Up = Keys.O, Down = Keys.L };
}
void MediaPlayer_MediaStateChanged(object sender, System.EventArgs e)
{
MediaPlayer.Volume -= 0.1f;
MediaPlayer.Play(song);
}
public void Update(GameTime gameTime)
{
// TODO: update player movement
_paddle1.Update(gameTime);
_paddle2.Update(gameTime);
// TODO: update ball physics
_ball.Update(gameTime);
f_ball.Update(gameTime);
f_paddle1.Update(gameTime);
// Ball hits paddle 1
if (_ball.BoundingBox.Intersects(_paddle1.BoundingBox))
{
_ball.Direction = new Vector2(_ball.Direction.X * -1, _ball.Direction.Y + _paddle1.Direction.Y);
}
if (_ball.BoundingBox.Intersects(_paddle2.BoundingBox))
{
_ball.Direction = new Vector2(_ball.Direction.X * -1, _ball.Direction.Y + _paddle2.Direction.Y);
}
// Ball reaches left side of screen
if (_ball.Position.X < 0)
{
//_ball.Position = new Vector2(0, _ball.Position.Y);
//_ball.Direction = new Vector2(-1 * _ball.Direction.X,
// _ball.Direction.Y);
_score2++;
ResetGame();
}
// Ball reaches right side of screen
if (_ball.Position.X > _resolution.X -
_ball.BoundingBox.Width)
{
//_ball.Position = new Vector2(
// _resolution.X - _ball.BoundingBox.Width,
// _ball.Position.Y);
//_ball.Direction = new Vector2(
// -1 * _ball.Direction.X, _ball.Direction.Y);
_score1++;
ResetGame();
}
// Ball reaches top side of screen
if (_ball.Position.Y < 0)
{
_ball.Position = new Vector2(_ball.Position.X, 0);
_ball.Direction = new Vector2(
_ball.Direction.X,
_ball.Direction.Y * -1);
}
// Ball reaches bottom side of screen
if (_ball.Position.Y > _resolution.Y -
_ball.BoundingBox.Height)
{
_ball.Position = new Vector2(
_ball.Position.X, _resolution.Y -
_ball.BoundingBox.Height);
_ball.Direction = new Vector2(
_ball.Direction.X, -1 * _ball.Direction.Y);
}
}
public void ResetGame()
{
_paddle1.Position = new Vector2(0, (_resolution.Y - _paddle1.Texture.Height) / 2);
_paddle2.Position = new Vector2(_resolution.X - _paddle2.Texture.Width, (_resolution.Y - _paddle2.Texture.Height) / 2);
_ball.Position = new Vector2(((_resolution.X - _ball.Texture.Width) / 2), ((_resolution.Y - _ball.Texture.Height) / 2));
// Starting direction for ball
_ball.Direction = new Vector2(1, 0);
_ball.Speed = 440f;
}
public void Draw(GameTime gameTime, SpriteBatch batch)
{
batch.Begin();
//_paddle1.Draw(gameTime, batch);
_paddle2.Draw(gameTime, batch);
// Testing different shader values on paddles
f_paddle1.Draw(gameTime, batch);
r_test.Draw(gameTime, batch);
// normal ball
//_ball.Draw(gameTime, batch);
// fade ball
f_ball.Draw(gameTime, batch);
batch.DrawString(f_posX, "Pos X" + _ball.Direction.X.ToString(), new Vector2(100, 100), Color.White);
batch.DrawString(f_posX, "Pos Y" + _ball.Direction.Y.ToString(), new Vector2(100, 150), Color.White);
batch.DrawString(f_posX, "Pos Y" + _paddle2.Direction.Y.ToString(), new Vector2(100, 200), Color.White);
batch.DrawString(f_score, "SCORE: " + _score1.ToString() + " | " + _score2.ToString(), new Vector2((_resolution.X / 2), 100), Color.AliceBlue);
batch.End();
}
}
}