Skip to content

Commit

Permalink
Added TimedEvent in FlappyNez
Browse files Browse the repository at this point in the history
  • Loading branch information
salvadorbs committed Oct 14, 2016
1 parent 3eee071 commit c6d8abf
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 56 deletions.
43 changes: 0 additions & 43 deletions FlappyNez/Entities/RockManager.cs

This file was deleted.

50 changes: 50 additions & 0 deletions FlappyNez/Entities/TimedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Nez;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlappyNez.Entities
{
class TimedEvent : Entity
{
float _previousRefreshTime = -0f;
float _refreshTime;

public event EventHandler Interval;

public TimedEvent(float refreshTime) : base("TimedEvent")
{
_refreshTime = refreshTime;
}

public override void onAddedToScene()
{
base.onAddedToScene();
OnInterval(new EventArgs());
}

public override void update()
{
base.update();

if (Time.time - _previousRefreshTime > _refreshTime)
{
_previousRefreshTime = Time.time;

OnInterval(new EventArgs( /* roba */ ));
}
}

protected virtual void OnInterval(EventArgs e)
{
EventHandler handler = Interval;

if (handler != null)
{
handler(this, e);
}
}
}
}
9 changes: 0 additions & 9 deletions FlappyNez/Factories/EntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public enum EntityType
{
Background,
Player,
RockUp,
RockDown,
RockManager,
Terrain
}

Expand All @@ -35,12 +32,6 @@ public Entity CreateEntity(EntityType Type)
return new Background();
case EntityType.Player:
return new Player();
case EntityType.RockUp:
return new Rock();
case EntityType.RockDown:
return new Rock(false);
case EntityType.RockManager:
return new RockManager();
case EntityType.Terrain:
return new Terrain();
default:
Expand Down
26 changes: 26 additions & 0 deletions FlappyNez/Factories/RockFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Microsoft.Xna.Framework;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using FlappyNez.Entities;

namespace FlappyNez.Factories
{
class RockFactory
{
Scene _scene;

public RockFactory(Scene scene)
{
_scene = scene;
}

public void CreateRocks(object sender, EventArgs e)
{
_scene.addEntity(new Rock());
_scene.addEntity(new Rock(false));
}
}
}
3 changes: 2 additions & 1 deletion FlappyNez/FlappyNez.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
</Compile>
<Compile Include="Entities\Background.cs" />
<Compile Include="Entities\Player.cs" />
<Compile Include="Entities\RockManager.cs" />
<Compile Include="Factories\RockFactory.cs" />
<Compile Include="Entities\Terrain.cs" />
<Compile Include="Entities\TimedEvent.cs" />
<Compile Include="Factories\EntityFactory.cs" />
<Compile Include="Game.cs" />
<Compile Include="Program.cs" />
Expand Down
12 changes: 9 additions & 3 deletions FlappyNez/Scenes/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ namespace FlappyNez.Scenes
{
class Level : Scene
{
private RockFactory _rockManager;
private TimedEvent _timedEvent;

public override void initialize()
{
_rockManager = new RockFactory(this);
_timedEvent = new TimedEvent((Screen.width / Constants.ObstaclesSpeed) - 1);
_timedEvent.Interval += _rockManager.CreateRocks;


//Entities
addEntity(EntityFactory.Instance.CreateEntity(EntityType.Background));
//addEntity(EntityFactory.Instance.CreateEntity(EntityType.Player));
addEntity(EntityFactory.Instance.CreateEntity(EntityType.Terrain));
addEntity(EntityFactory.Instance.CreateEntity(EntityType.RockUp));
addEntity(EntityFactory.Instance.CreateEntity(EntityType.RockDown));
addEntity(EntityFactory.Instance.CreateEntity(EntityType.RockManager));
addEntity(_timedEvent);
}
}
}

0 comments on commit c6d8abf

Please sign in to comment.