Skip to content

Commit

Permalink
Fixed StyleCop warnings (flappynez)
Browse files Browse the repository at this point in the history
Added random offset in Rocks
  • Loading branch information
salvadorbs committed Oct 15, 2016
1 parent a671640 commit 1cd0452
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 113 deletions.
11 changes: 4 additions & 7 deletions FlappyNez/Components/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using Microsoft.Xna.Framework;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Nez;

namespace FlappyNez.Components
{
Expand All @@ -13,7 +10,7 @@ class PlayerController : Component, IUpdatable

public override void onAddedToEntity()
{
//Keyboard/Mouse/Touch/Joystick
// Keyboard/Mouse/Touch/Joystick
Input.touch.enableTouchSupport();
_input.nodes.Add(new Nez.VirtualButton.KeyboardKey(Keys.Space));
_input.nodes.Add(new Nez.VirtualButton.MouseLeftButton());
Expand All @@ -26,7 +23,7 @@ void IUpdatable.update()

if (rigidbody != null)
{
if ((_input.isPressed) && (rigidbody.velocity.Y >= 0))
if (_input.isPressed && (rigidbody.velocity.Y >= 0))
{
rigidbody.setVelocity(Vector2.Zero);
rigidbody.addImpulse(Constants.PlayerSpeed);
Expand Down
7 changes: 4 additions & 3 deletions FlappyNez/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace FlappyNez
{
static class Constants
{
public static readonly float ObstaclesSpeed = 50f;
public static readonly float BackgroundSpeed = 100f;
public static readonly float TerrainSpeed = 100f;
public const float ObstaclesSpeed = 50f;
public const float BackgroundSpeed = 100f;
public const float TerrainSpeed = 100f;
public const int GapHeight = 0;

public static readonly Vector2 PlayerInitialPos = Screen.center;
public static readonly Vector2 PlayerSpeed = new Vector2(0, -6);
Expand Down
2 changes: 1 addition & 1 deletion FlappyNez/ContentPathGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


// <auto-generated/>

namespace Nez
{
Expand Down
2 changes: 1 addition & 1 deletion FlappyNez/ContentPathGenerator.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<#@ import namespace="System.Text.RegularExpressions" #>

<# var sourceFolders = new string[] { "Content/bin/DesktopGL", "Content" }; #>

// <auto-generated/>

namespace Nez
{
Expand Down
12 changes: 5 additions & 7 deletions FlappyNez/Entities/Background.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace FlappyNez.Entities
{
class Background : Entity
{
public Background() : base("Background")
{
//Position in screen center (important for y coordinate)
// Position in screen center (important for y coordinate)
transform.position = Screen.center;
}

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

//Load background sprite (scrolling background with a specific speed)
// Load background sprite (scrolling background with a specific speed)
var sprite = new ScrollingSprite(scene.content.Load<Texture2D>(Content.Terrain.background))
{
scrollSpeedX = Constants.BackgroundSpeed
};
//Add sprite component with renderLayer to 2 (in this way, background renders are in the back)

// Add sprite component with renderLayer to 2 (in this way, background renders are in the back)
addComponent(sprite)
.renderLayer = 2;
}
Expand Down
18 changes: 8 additions & 10 deletions FlappyNez/Entities/Player.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
using System;
using FlappyNez.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using FlappyNez.Components;

namespace FlappyNez.Entities
{
class Player : Entity
{
public Player() : base("Plane")
{
//Collider component
// Collider component
addCollider(new BoxCollider());

//RigidBody component
//No need to set a velocity because gravity does all the work
// RigidBody component
// No need to set a velocity because gravity does all the work
var rigidbody = new ArcadeRigidbody()
.setMass(1f)
.setVelocity(Vector2.Zero);
addComponent(rigidbody);
addComponent(new PlayerController());

//Position
// Position
transform.position = Constants.PlayerInitialPos;
}

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

//Load sprite and add it in entity
//TODO: Simple plane animation and rotation (down and up)
// Load sprite and add it in entity
// TODO: Simple plane animation and rotation (down and up)
addComponent(new Sprite(scene.content.Load<Texture2D>(Content.Planes.planeBlue1)));
}
}
Expand Down
51 changes: 29 additions & 22 deletions FlappyNez/Entities/Rock.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace FlappyNez.Entities
{
class Rock : Entity
{
string _spriteName = string.Empty;
Sprite _Sprite;
Sprite _sprite;
public bool IsUp;
int _offset;
Mover _mover;

public Rock(bool IsUp = true) : base()
public Rock(int offset = 0, bool isUp = true) : base()
{
this.IsUp = IsUp;
if (IsUp)
this.IsUp = isUp;
_offset = offset;

if (isUp)
{
name = "Rock_Down";
_spriteName = Content.Terrain.rockGrassDown;
Expand All @@ -28,39 +29,45 @@ public Rock(bool IsUp = true) : base()
_spriteName = Content.Terrain.rockGrass;
}

//Mover component
// Mover component
_mover = addComponent(new Mover());
}

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

//Load sprite and add it in entity
_Sprite = addComponent(new Sprite(scene.content.Load<Texture2D>(_spriteName)));
//RenderLayer to 1 because need drawing rock behind terrain
_Sprite.renderLayer = 1;
// Load sprite and add it in entity
_sprite = addComponent(new Sprite(scene.content.Load<Texture2D>(_spriteName)));

//Polygon Collider
// RenderLayer to 1 because need drawing rock behind terrain
_sprite.renderLayer = 1;

// Polygon Collider
var collider = addCollider(new PolygonCollider(GetRockVertices()));
//Must colliders with only layer 0 (in other words with plane)

// Must colliders with only layer 0 (in other words with plane only)
Flags.setFlagExclusive(ref collider.collidesWithLayers, 0);

ResetPosition();
}

private void ResetPosition()
{
transform.position = new Vector2((Screen.width + (_Sprite.width / 2)),
IsUp ? (_Sprite.height / 4) : (Screen.height - (_Sprite.height / 4)));
var _gapHeight = MathHelper.Clamp(Constants.GapHeight, (_sprite.height / 2), _sprite.height);
var realRockHeight = (Screen.height - _gapHeight) / 2;
var nominalRockHeight = (_sprite.height / 2) - (_sprite.height - realRockHeight);

transform.position = new Vector2((Screen.width + (_sprite.width / 2)),
IsUp ? (_offset + nominalRockHeight) : (_offset + Screen.height - nominalRockHeight));
}

private Vector2[] GetRockVertices()
{
//TODO: This code is dirty (and hard coded) solution. Rewrite this!
// Must extract vertices from Texture2 as Farseer Physics does
var height = _Sprite.height * (IsUp ? -1 : 1);
var width = _Sprite.width * (IsUp ? 1 : -1);
// TODO: This code is dirty (and hard coded) solution. Rewrite this!
// Must extract vertices from Texture2 as Farseer Physics does
var height = _sprite.height * (IsUp ? -1 : 1);
var width = _sprite.width * (IsUp ? 1 : -1);

var verts = new Vector2[4];
verts[(IsUp ? 1 : 0)] = new Vector2(8, -(height / 2));
Expand All @@ -78,7 +85,7 @@ public override void update()
CollisionResult res;
_mover.move(new Vector2(-1, 0) * Constants.ObstaclesSpeed * Time.deltaTime, out res);

if (transform.position.X <= -(_Sprite.width / 2))
if (transform.position.X <= -(_sprite.width / 2))
{
this.destroy();
}
Expand Down
14 changes: 6 additions & 8 deletions FlappyNez/Entities/Terrain.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace FlappyNez.Entities
{
class Terrain : Entity
{
public Terrain() : base("Terrain")
{
//Collider component
// Collider component
var collider = addCollider(new BoxCollider());

//Set physics layer to 1 - avoid check collision with rocks
// Set physics layer to 1 - avoid check collision with rocks
Flags.setFlagExclusive(ref collider.physicsLayer, 1);
}

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

//Load scrolling sprite
// Load scrolling sprite
var _sprite = new ScrollingSprite(scene.content.Load<Texture2D>(Content.Terrain.groundGrass))
{
scrollSpeedX = Constants.TerrainSpeed
};
addComponent(_sprite);

//Position
// Position
transform.position = new Vector2((_sprite.width / 2), (Screen.height - (_sprite.height / 2)));
}
}
Expand Down
13 changes: 5 additions & 8 deletions FlappyNez/Entities/TimedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using Nez;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using Nez;

namespace FlappyNez.Entities
{
Expand All @@ -22,6 +18,7 @@ public TimedEvent(float refreshTime) : base("TimedEvent")
public override void onAddedToScene()
{
base.onAddedToScene();

OnInterval(new EventArgs());
}

Expand All @@ -33,13 +30,13 @@ public override void update()
{
_previousRefreshTime = Time.time;

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

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

if (handler != null)
{
Expand Down
19 changes: 4 additions & 15 deletions FlappyNez/Factories/EntityFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nez;
using FlappyNez.Entities;
using Nez;

namespace FlappyNez.Factories
{
Expand All @@ -17,16 +13,9 @@ public enum EntityType

class EntityFactory
{
private static EntityFactory _instance = new EntityFactory();

public static EntityFactory Instance
{
get { return _instance; }
}

public Entity CreateEntity(EntityType Type)
public static Entity CreateEntity(EntityType type)
{
switch (Type)
switch (type)
{
case EntityType.Background:
return new Background();
Expand All @@ -35,7 +24,7 @@ public Entity CreateEntity(EntityType Type)
case EntityType.Terrain:
return new Terrain();
default:
throw new ArgumentException("type");
throw new ArgumentException("entity type not supported");
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions FlappyNez/Factories/RockFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using Microsoft.Xna.Framework;
using Nez;
using Nez.Sprites;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using FlappyNez.Entities;
using Nez;

namespace FlappyNez.Factories
{
Expand All @@ -19,8 +15,10 @@ public RockFactory(Scene scene)

public void CreateRocks(object sender, EventArgs e)
{
_scene.addEntity(new Rock());
_scene.addEntity(new Rock(false));
var _offset = Nez.Random.range(-60, 60);

_scene.addEntity(new Rock(_offset));
_scene.addEntity(new Rock(_offset, false));
}
}
}
Loading

0 comments on commit 1cd0452

Please sign in to comment.