-
Notifications
You must be signed in to change notification settings - Fork 0
Supporting gameplay components
Colliders are used to detect collisions between objects. They can be initialized in the Init
method and updated in the Update
method.
Besides, they can be drawn in the Draw
method for debugging purposes:
private Vector2 pos;
private Collider c;
private Collider c2;
/* Initialize game vars and load assets. */
public void Init()
{
SetScreenSize(800, 600);
// Create position for collider c
pos = new Vector2(100, 0);
// Setup colliders
c = new Collider(this, pos, 50, 50);
c2 = new Collider(this, new Vector2(100, 100), 50, 50);
}
/* Update game logic. */
public void Update(GameTime deltaTime)
{
// Stop if c collides with c2
if (c.CollidesWith(c2))
{
return;
}
// Move c down
pos.Y += 0.5f;
c.Update(pos);
}
/* Draw objects/backdrop. */
public void Draw()
{
// Draw colliders
c.Draw();
c2.Draw();
}
Timers are used to execute code after a certain amount of time has passed. Timer
objects can be used to manage timers. For initialization, the timer requires a time argument (in milliseconds) to wait before executing the code. The timer object can be updated in an Update
method.
public partial class Game
{
private Timer _testTimer;
/* Initialize game vars and load assets. */
public void Init()
{
_testTimer = new Timer(1000);
}
/* Update game logic. */
public void Update(GameTime deltaTime)
{
_testTimer.Update(deltaTime);
if (_testTimer.TimerOver())
{
Console.WriteLine("Timer over!");
_testTimer.ResetTimer();
}
}
/* Draw objects/backdrop. */
public void Draw()
{
}
}
The particle system is used to create particle effects. It makes use of the ParticleManager
class, which is initialized
in the Init
method. Particles are updated in the Update
method and drawn in the Draw
method:
private ParticleManager _particleManager;
/* Initialize game vars and load assets. */
public void Init()
{
SetScreenSize(800, 600);
SetBackgroundColor(Color.White);
_particleManager = new ParticleManager(this);
}
/* Update game logic. */
public void Update(GameTime gameTime)
{
_particleManager.CreateParticle(
null,
new Vector2(400, 300),
new Vector2(0, -1),
Color.Black,
5,
3);
_particleManager.Update(gameTime);
}
/* Draw objects/backdrop. */
public void Draw()
{
_particleManager.Draw();
}
To create custom particles (recommended), create a class that inherits from Particle
. Then, override the Update
method to define the particle's behavior:
public class MyParticle : Particle
{
public MyParticle(Game game, Texture2D texture, Vector2 position, Vector2 velocity, Color color, float scale, float lifeTime)
: base(game, texture, position, velocity, color, scale, lifeTime)
{
}
public override void Update(GameTime gameTime)
{
// Define particle behavior here
}
}
MonoZenith has built-in support for controllers. Properties and methods for controllers can be found in the Game
class.
Before using controller properties/methods, make sure to check whether a controller is connected using the ControllerConnected
property.
If connected, the state of the controller can be accessed using the GetGamePadState
method. This method returns a GamePadState
object,
which contains information about the state of the controller. The GamePadState
object can be used to check whether a button is pressed,
or whether the left or right trigger is pressed. Enumerators are built-in for the PlayStation DualSense and XBOX controller buttons.
The GamePadState
object can also be used to get the position of the left and right thumbsticks.
The controller support has been tested with the PlayStation DualSense controller. The XBOX controller has not been tested yet,
but should work. This also applies to the PlayStation DualShock 4 controller. Support for other controllers might be added in the future.
/* Initialize game vars and load assets. */
public void Init()
{
}
/* Update game logic. */
public void Update(GameTime deltaTime)
{
if (ControllerConnected)
{
GamePadState state = this.GetGamePadState();
VibrateController(0.5f, 0.5f);
if (state.IsButtonDown((Buttons)DualSenseButtons.Cross))
{
DebugLog("Jump!");
}
if (HasDPad && HasLeftStick)
{
if (state.IsButtonDown((Buttons)DualSenseButtons.Right) ||
state.ThumbSticks.Left.X > 0.5f)
{
DebugLog("Move right!");
}
}
}
}
/* Draw objects/backdrop. */
public void Draw()
{
}