Mr Nibbles – Mobile Testing

Mr Nibbles – Mobile Testing

This will be the last update for a while as I am heading up north WA for 3 weeks from tomorrow but before I went I wanted to get the game running on my iphone so I could test it while I was away. After a few accelerometer tweaks im glad to say that the game works just fine on Android and iOS:

The frame rate is great (on my iphone 5 at least) so im happy with that. While i’m away ill do some pen-and-paper level design, kind of tricky when the levels are 3D but ill give it a go!

Mr Nibbles 3D – Skyboxes and Fake Shadows

Mr Nibbles 3D – Skyboxes and Fake Shadows

After that short interlude im back to games development. This time its more work on Mr Nibbles 3D.

I decided to experiment with Skyboxes. I have never worked with them in Unity so I didn’t know how hard they would be to get working. I shouldn’t have been worried, as with most thing in Unity, it was a piece of cake. Unity even ships with some default Skyboxes so I chucked one of those in.

In the above video I also demonstrate another thing I worked on, shadows. Unfortunately realtime shadows are a Unity Pro feature which I currently don’t own so I decided to have a go at faking shadows.

Its surprisingly simple, I just cast a ray in the direction of gravity from Mr Nibbles until it collides with a tile, when that happens I place a “shadow” sprite:

screenshot_011

The further the distance from ray to collision the lower the alpha transparency value to indicate a fainter shadow. Its surprising how much more realism this instantly adds to the game.

This method does however have its problems. Such as when Mr Nibbles overhangs a cliff:

screenshot_012

You can see the problem in the screenshot above, Mr Nibble’s shadow should wrap around the tile rather than hang in the air. One solution to this which I experimented with is to use projectors. Projectors are like light sources but in reverse, they cast shadows onto the scene. By placing one so that it followed Mr Nibbles and pointed in the direction of gravity you could create the effect of a shadow:

screenshot_013

You can see that the shadow no longer overhangs the tile as it is cast directly when rendering rather than being a separate game object. Unfortunately it turns out that for this to work you cannot cast onto Transparent textures, and as all the tiles in Mr Nibbles must support transparency so that they can fade out when Mr Nibbles obscures them this technique could work.

Oh well. Ill have to wait till I have the pro version of Unity to implement dynamic shadows, either that or I can try to write some code to minimize the overhang problem.

Unity Ashteroids – Ash Game Framework in Unity

Unity Ashteroids – Ash Game Framework in Unity

I decided to take a break from the game development today to scratch an itch I had had for a while.

Its no secret that im a fan of Entity-Component systems having used various flavours of them for years. My particular favourite however is found in Richard Lord’s Ash Framework. I have used it on numerous projects including my 3-game prototype challenge.

Ash Framework however is written for the Flash platform. Though there are ports for other platforms, no one has yet done one for Unity. It gets talked about on the Ash mailing list every now and then but often dismissed as either impossible or pointless as Unity already has an Entity framework built into it.

Well after a recent discussion I decided I wanted to have a go at it anyway. It turns out its actually not that hard to implement.

Starting off with David Arno’s .Net port of Ash I wrote two small classes that wrap the Ash Engine and Ash Entity.

Ash Entity is the key piece to the puzzle. You add it as a component to your game object:

screenshot_003

It then checks to see which Unity components have been added or removed each frame and updates the Ash Entity reference. Its simple but it works! It even includes the default Unity Components so that means you don’t need a base “AshComponent”.

The AshEngine serves as the root of the hierarchy. So long as you place entities below it they will be able to find the engine. This makes it really easy and convenient to create scenes at design time and have them work when you hit play:

screenshot_005

From that simple foundation I decided to have a go at porting the entire of Richard’s Asteroids example over to Unity. I was surprised at how little issues I ran into. You can give it a play here:

http://mikecann.co.uk/projects/UnityAshteroids/UnityAshteroids.html
Use arrow keys to move and space to shoot.

It all works surprisingly well. The code is really clean and very simmilar to the original.

The only changes I needed to make from the .Net port was to change some “internals” to “public” and changed the type of the update variable from double to float as Unity uses floats for everything not doubles.

Instead of manually defining the components in “EntityCreator” I use Unity’s prefab system which makes it really nice and easy to edit properties in the inspector in the editor:

screenshot_004

Then load them the usual Unity way using Resources.Load().

Systems are just pure code and added to the Engine as usual:

public class Asteroids : AshGame
{
	private EntityCreator creator;
	private GameConfig config;

	void Awake()
	{
		creator = new EntityCreator(this);
		config = new GameConfig();

		var size = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
		config.Bounds = new Bounds(Vector3.zero, new Vector3(size.x*2, size.y*2));

		Engine.AddSystem(new WaitForStartSystem(creator), SystemPriorities.PreUpdate);
		Engine.AddSystem(new GameManagerSystem(creator, config), SystemPriorities.PreUpdate);
		Engine.AddSystem(new MotionControlSystem(), SystemPriorities.Update);
		Engine.AddSystem(new GunControlSystem(creator), SystemPriorities.Update);
		Engine.AddSystem(new BulletAgeSystem(creator), SystemPriorities.Update);
		Engine.AddSystem(new DeathThroesSystem(creator), SystemPriorities.Update);
		Engine.AddSystem(new MovementSystem(config), SystemPriorities.Move);
		Engine.AddSystem(new CollisionSystem(creator), SystemPriorities.ResolveCollisions);
		Engine.AddSystem(new HudSystem(), SystemPriorities.Animate);
		Engine.AddSystem(new AudioSystem(), SystemPriorities.Render);   
		
		creator.CreateWaitForClick();
		creator.CreateGame();
	}
}  

The changes to the Asteroids code in general are minimal. I use Unity’s components wherever possible, so things like Position and Movement become Transform and Rigidbody2D. Rendering is taken care of by Unity so I didn’t need any RenderingSystem or Display component.

The code is available on GitHub for your perusal: https://github.com/mikecann/UnityAshteroids

I was actually surprised I was able to get this all working without any problems. There are improvements that could be made:

Performance of AshEntity

AshEntity is the core to the whole thing and unfortunately it probably isn’t the most performant. For each entity in the game it checks every frame to see if any components were added or removed. It doesn’t do this in the best possible way so there is definitely room to improve this.

Sugar

There is plenty of room for syntax sugar thanks to the superior C# language over Actionscript 3. NodeList could be made generic and a generic ListIteratingSystem could be developed and perhaps even a way of eliminating nodes altogether. Also retrieving components from Ash Entity could be made simpler by removing the need to pass in the type param when calling Entity.Get<>().

Finite State Machines

As the .Net port does not include Richard’s FSM code I worked around the problem by destroying and creating new prefabs. This works quite well as it allows you to use the inspector to visually design the prefabs however a nice addition would be some editor tools and code for FSMs in Unity.

Unit Tests

I copied over the .Net tests from David’s port and got them running in unity using Unity Test tools without any problems however my Ash Unity code isn’t tested. There isn’t much of it but it probably should be covered.