Reference:
This is the third tutorial on how to work with the
XNACS1Lib
library. It is assumed you have read and understand the previous
two tutorials:
Goals
:
This tutorial concentrates on illustrating working with
PrimitiveSet
for controlling the visibility (membership with AutoDrawSet) of any related set of primitives, and on
the
Random
number utilities:
PrimitiveSet:
Logical entity for
grouping
related
Primitive
for the purpose of showing/hiding these Primitives.
As a programmer you are responsible for ensuring each Primitive belongs to at most one PrimitiveSet.
Random number utilities:
randomness is a convenient tool for many
interactive graphical applications. XNACS1Lib supports random number
generation.
1. Obtain the example code
Here is the zip file to the source
files and compiled executable of this example.
Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram
folder, the EXE folder contains the compiled program and you can double click on the .sln
file to work with the source code.
If you double click and run the executable program, you will observe:
This application is almost identical to that from the second tutorial, except
two important points:
Button-X: hides/un-hides the ladder and the pink-ish circle.
Random speed of the soccer ball after each collision with the ladder.
In this tutorial, we will examine the details of implementing these
functionality.
2. The Source Code Files/Structure
Take a look
at the
ExampleProgram
folder that you have unzipped and you will see a
structure identical to that of previous tutorials. Once again, you can double
click on the
*.sln
file to launch the IDE, we will concentrate on the
Game1.cs
file where all the relevant source code are
located.
3. Examine Game1.cs Structure:
When you open the
Game1.cs
file in the IDE, you will see source code
structure that is very similar to that of the second tutorial:
#region
Reference to libraries
namespace
XNACS1Lib_PrimitiveSet {
public
class
Game1
:
XNACS1Base
{
#region
Instance Variables
#endregion
protected
override
void
InitializeWorld()
protected
override
void
UpdateWorld()
}
}
Once again, we will examine each of :
Instance Variables,
InitializeWorld()
, and
UpdateWorld()
in details and understand how
to implement the behaviors we have observed.
4. Examine Instance Variables of Game1.cs
When you expand the
Instance Variable
region, you will observe:
#region
Instance Variables
...
SPEED ...
and
A B C D
that are identical to previous tutorial ...
XNACS1PrimitiveSet
m_VisibleSet;
// E. Button-X click makes this set invisible
bool
m_SetIsVisible;
// Records if
the set is currently visible
#endregion
The five sets of instance variables: A to E where variable sets A to D are
identical to previous tutorial.
E:
(
m_VisibleSet
) is a logical set
that lets us insert related Primitives and control the visibility of the
Primitives as a set.
5. Examine InitializeWorld() function of Game1.cs
The
InitializeWorld()
function is once again very similar to previous
tutorial:
// A. Create the initialize the rotating
ladder
...
// B. Create and initialize the bouncing
soccer ball
...
// C. Create and initialize the circle
controlled by the right thumb stick
...
// D. Create and initialize the eraser
Rectangle
...
// E. Insert: RightThumbCircle,
and Ladder into the m_VisibleSet
m_VisibleSet =
new
XNACS1
PrimitiveSet
();
m_VisibleSet.AddToSet(m_RightThumbCircle);
m_VisibleSet.AddToSet(m_RotatingLadder);
m_SetISVisible =
true
;
}
At label
E
, after allocating the memory for the
m_VisibleSet
, we
inserted the RightThumbCircle and the RotatingLadder to be members of the
m_VisibleSet
, and record the fact that current the
m_VisibleSet
is
visible by setting
m_SetIsVisibl
e to true.
6. Examine UpdateWorld() function of Game1.cs
Now in the UpdateWorld() function, we control the visibility of the
m_VisibleSet
and the speed of the soccer ball after collision:
protected
override
void
UpdateWorld() {
if
(GamePad.ButtonBackClicked())
this.Exit();
#region
A.
Update the Rotating ladder
...
#region
B.
...
#region
C.
...
#region
D.
...
#
region
E.
toggle VisibleSet by the X-Button
if
(GamePad.ButtonXClicked())
{
if
(m_SetIsVisible)
m_VisibleSet.RemoveAllFromAutoDrawSet();
else
m_VisibleSet.AddAllToAutoDrawSet();
m_SetIsVisible = !m_SetIsVisible;
}
#endregion
#
region
F.
Collide the soccer with the rotating ladder
bool
collided =
m_SoccerBall.Collided(m_RotatingLadder);
if
(collided) {
m_SoccerBall.VelocityDirection = m_SoccerBall.Center -
m_RotatingLadder.Center;
m_SoccerBall.Speed = SPEED *
RandomFloat(0.5f, 2.0f)
;
}
...
#endregion
EchoToTopStatus(...);
}
Region E:
When the X-Button is clicked, we check if
m_SetISVisible
, if so, we "RemoveAllFromAutoDrawSet()"
to hide the RightThumbCircle and the Ladder. Otherwise we call "AddAllToAutoDrawSet()"
to show the two primitives.
Region F:
When collision between the soccer and the ladder is
detected, the speed of the soccer is
randomized
by the "RandomFloat()"
function.
This work is supported in part by a grant from
Microsoft Research under the Computer Gaming Curriculum in Computer
Science RFP, Award Number 15871 and 16531.