-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRock.cs
More file actions
127 lines (105 loc) · 3.53 KB
/
Rock.cs
File metadata and controls
127 lines (105 loc) · 3.53 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ProgrammingAssignment3
{
/// <summary>
/// A rock
/// </summary>
public class Rock
{
#region Fields
// drawing support
Texture2D sprite;
Rectangle drawRectangle;
// moving support
Vector2 velocity;
// window containment support
int windowWidth;
int windowHeight;
bool outsideWindow = false;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="sprite">sprite for the rock</param>
/// <param name="location">location of the center of the rock</param>
/// <param name="velocity">velocity of the rock</param>
/// <param name="windowWidth">window width</param>
/// <param name="windowHeight">window height</param>
public Rock(Texture2D sprite, Vector2 location, Vector2 velocity,
int windowWidth, int windowHeight)
{
// save window dimensions
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
// save sprite and set draw rectangle
this.sprite = sprite;
drawRectangle = new Rectangle((int)location.X - sprite.Width / 2, (int)location.Y - sprite.Height / 2, sprite.Width, sprite.Height);
// save velocity
this.velocity = velocity;
}
#endregion
#region Properties
/// <summary>
/// Sets the rock's velocity
/// </summary>
public Vector2 Velocity
{
set
{
velocity.X = value.X;
velocity.Y = value.Y;
}
}
/// <summary>
/// Gets whether or not the rock is outside the window
/// </summary>
public bool OutsideWindow
{
get { return outsideWindow; }
}
#endregion
#region Methods
/// <summary>
/// Updates the rock
/// </summary>
/// <param name="gameTime">game time</param>
public void Update(GameTime gameTime)
{
// updating rock's location if the rock is inside the window
if (outsideWindow == false)
{
drawRectangle.X += (int)(velocity.X * gameTime.ElapsedGameTime.Milliseconds);
drawRectangle.Y += (int)(velocity.Y * gameTime.ElapsedGameTime.Milliseconds);
}
// checking if the rock is outside the window
Rectangle window = new Rectangle(0, 0, windowWidth, windowHeight);
if (drawRectangle.Top > window.Bottom)
{
outsideWindow = true;
}
else if (drawRectangle.Bottom < window.Top)
{
outsideWindow = true;
}
}
/// <summary>
/// Draws the rock
/// </summary>
/// <param name="spriteBatch">sprite batch</param>
public void Draw(SpriteBatch spriteBatch)
{
// checking if the rock is in the window and drawing the rock
if (outsideWindow == false)
{
spriteBatch.Draw(sprite, drawRectangle, Color.White);
}
}
#endregion
}
}