80
Introductions… Hi, I’m Rob Hawkes and I’m here today so we can explore gaming with HTML5 and JavaScript.

HTML5 & JavaScript Games

Embed Size (px)

DESCRIPTION

During this intimate lunchtime session, Rob Hawkes will bring you up to speed with the HTML5 and JavaScript game development scene. He'll show off some of the best games that are already out there, highlight the key lessons that he's learnt, and highlight the technologies and game engines that you need to be looking at. Interested in making games on the Web? Definitely don't miss this talk.

Citation preview

Page 1: HTML5 & JavaScript Games

Introductions…

Hi, I’m Rob Hawkes and I’m here today so we can explore gaming with HTML5 and JavaScript.

Page 2: HTML5 & JavaScript Games

For those who don’t know much about about me…

I’m a Technical Evangelist at Mozilla, which means that it’s my job to engage with developers about cool new technologies on the Web.

Page 3: HTML5 & JavaScript Games

1ST-CLASS HONOURSInteractive Media Production

And as of 4am this morning (midday in the UK) I found out that I achieved a First-class Honours in the university degree that I’ve been studying for the past three years.

Which has certainly helped make all the time and effort feel a little more worth while!

But I digress…

Page 4: HTML5 & JavaScript Games

All of this means that I have a slight addiction to experimentation and mucking about with code, particularly if it involves anything visual, or HTML5…

And for those of you who doubt my dedication to HTML5…

A while ago my girlfriend made me this chicken and leek pie with the HTML5 logo on top (which was delicious).

It’s HTML… Pive… get it?

Page 5: HTML5 & JavaScript Games

I don’t actually remember when I first started playing games, although I know that I started with consoles… ZX Spectrum…

Page 6: HTML5 & JavaScript Games

…SNES…

Page 7: HTML5 & JavaScript Games

Megadrive, etc.

Page 8: HTML5 & JavaScript Games

And spread throughout that time I dabbled in PC gaming, starting with games like Sim City…

Page 9: HTML5 & JavaScript Games

…Megarace…

Page 10: HTML5 & JavaScript Games

And playing Doom at my Dad’s Internet cafe.

What I’m getting at here is that gaming has been a big part of my life growing up, as it has been with a lot of other people as well.

They’re fun to play, and they’re surprisingly fun to make.

Page 11: HTML5 & JavaScript Games

THE TIME IS NOWThreshold of something cool

And we’re now on the threshold of something cool, being able to create awesome and addictive games with nothing more than the technologies that we normally used to make websites with.

Page 12: HTML5 & JavaScript Games

NOTABLE EVENTSRecent events in HTML5 gaming

Funding for HTML5 games companies.

Acquisition of HTML5 gaming engines, like Aves by Zynga, and Rocket Engine by Disney.

First large-scale HTML5 gaming conference this September, onGameStart in Poland.

Another large gaming conference announced in the last few days for the San Francisco area this November, it’s called New Game and it looks promising.

Facebook getting involved in HTML5 gaming performance.

Page 13: HTML5 & JavaScript Games

* made by Phil Banks (@emirpprime)

So why is HTML5 gaming so cool?

Because we can now use the technologies within the browser to make fast and enjoyable games.

And just to clarify, by HTML5 I'm referring to HTML5 and JavaScript.

Page 14: HTML5 & JavaScript Games

WRITE ONCEUse anywhere

Any device with a browser that supports HTML5 will run your game code, after all it’s just JavaScript and HTML.

You may have to implement little differences for variations in controls, like touch on mobile devices, but the majority of the code will remain the same.

Page 15: HTML5 & JavaScript Games

NO COMPILATIONSave time with development and testing

Doesn't require compiling, so development and testing can be extremely rapid.

These technologies encourage a culture of hacking stuff together quickly to experiment, then tidying things up later – or perhaps that's just me.

Either way, I like the rapid nature that JavaScript and HTML brings.

Page 16: HTML5 & JavaScript Games

LIGHTWEIGHTText and image files take up little room

JavaScript and HTML are just text files and images, which don't take up much room.

Page 17: HTML5 & JavaScript Games

OPEN DEVELOPMENTGives you a warm fuzzy feeling

It gives you a warm fuzzy feeling inside to be using open technologies

Page 18: HTML5 & JavaScript Games

But it’s not all rosy with HTML5, why would you not want to use it?

Here are just two of the major issues that are floating around right now.

Page 19: HTML5 & JavaScript Games

FULL COMPATIBILITYNot all browsers support everything

Not every browser supports every part of HTML5.

For example, canvas isn’t supported out of the box by any IE below 9. Although, you can use ExplorerCanvas to replicate canvas functionality, but it’s not ideal and as doesn’t perform as well.

WebSockets still isn’t supported by IE yet, nor is it supported in Android. But again, you can fake this by using Flash for the sockets, like with Socket.IO.

WebGL also has patchy support, with absolutely no support in current versions of IE, Safari, and Opera.

Page 20: HTML5 & JavaScript Games

NO DRMThese technologies aren’t made for it

If you need DRM or have a burning desire to hide absolutely everything about your code.

Remember, the code isn’t compiled, so where would the DRM go? The beauty of Web technologies is that they can be read as plain text by simply viewing the page source.

However, DRM isn't bulletproof in itself – you can still crack into Flash.

You can still obfuscate and minify your code if you think it will help.

Page 21: HTML5 & JavaScript Games

TECHNOLOGYSome of the tech involved in Web gaming

So there are a variety of technologies that are involved in HTML5 game development.

In case I haven’t made it obvious by now, all of these technologies are related to HTML or JavaScript.

Page 22: HTML5 & JavaScript Games

CANVAS & WEBGL2D and 3D graphics

Canvas for 2D graphics.

WebGL, which uses the canvas, for 3D graphics.

Page 23: HTML5 & JavaScript Games

HTML5 AUDIOSound effects and background music

HTML5 audio for game sound effects and background music.

Page 24: HTML5 & JavaScript Games

WEBSOCKETSMultiplayer communication

WebSockets are used for bi-directional communication between a client (the player) and a server.

By managing communication on the server you can create multiplayer games with relative ease.

Page 25: HTML5 & JavaScript Games

NODE.JSJavaScript on the server

Node is only really applicable if you’re making multiplayer games, as you’ll need a server to communicate with.

JavaScript on the server.

Asynchronous, event-based.

Great third-party modules, like Socket.IO for WebSockets.

Page 26: HTML5 & JavaScript Games

LOCAL STORAGEStoring data on the player device

Local Storage is great for storing data locally on the player device.

This way you can cache game data and allow the game to pick up where the player left off.

Page 27: HTML5 & JavaScript Games

MONGODB & REDISStoring data on the server

MongoDB and Redis are two examples of ways to store data remotely on the game server.

MongoDB is a document-oriented database, what’s often referred to as NoSQL.

Redis is a key-value data store and is much less structured than MongoDB, but it stores data in memory which makes it super fast.

Page 28: HTML5 & JavaScript Games

EXISTING GAMESSome of my favourite and best examples

There aren’t a huge amount of HTML5 games just yet, but I thought I’d show you a selection of my favourites.

Page 29: HTML5 & JavaScript Games

QUAKE IIGoogle port using Web technologies

Multiplayer Quake II remake with Google GWT (Google Web Toolkit)

Canvas & WebGLHTML5 audioWebSocketsLocal Storage

http://code.google.com/p/quake2-gwt-port/

Page 30: HTML5 & JavaScript Games

MINECRAFTWe’ll see this fully working in JavaScript soon

Minecraft map viewer, using WebGL.

This isn’t really a game, but it’s not a long way from becoming a JavaScript port of Minecraft.

http://alteredqualia.com/three/examples/geometry_minecraft_ao.html

Page 31: HTML5 & JavaScript Games

WORD²Massively multiplayer Scrabble

Word², massively multiplayer Scrabble.

Uses WebSockets for real-time communication.

http://wordsquared.com

Page 32: HTML5 & JavaScript Games

I love the map view.

Every single pixel on this view is a Scrabble tile.

Page 33: HTML5 & JavaScript Games

FREECIVOpen source clone of Civilisation

Freeciv, an open source clone of Civilisation that uses canvas and WebSockets.

3D WebGL version currently being worked on.

http://freeciv.net

Page 34: HTML5 & JavaScript Games

RAWKETSBecause I made it and it’s addictive

Rawkets is a multiplayer space shooter that I created while at university to explore canvas and WebSockets.

I’m now developing it a lot further as a proper game, and plan to use it to teach others how to make multiplayer HTML5 games.

Still not even at a beta release level yet, hence the bugs you can see in this video.

http://rawkets.com

Page 35: HTML5 & JavaScript Games

LOADS MOREGames directories are everywhere

Game directories specifically focused on HTML5 are popping up all over the place.

Also, Kongregate now accept HTML5 games so long as they run in an iframe.

html5games.com

Page 36: HTML5 & JavaScript Games

FUNDAMENTALSMaking games with HTML5

Here are a few fundamental aspects of a HTML5 game. It’s all pretty basic, but they’re useful to cover.

Not all of these are required, it depends what kind of game you're working on…

Page 37: HTML5 & JavaScript Games

GAME LOOPThe heart of the game

Most games will have a loop of some kind to manage all the logic and animation that needs to happen.

This is the heart of the game and is where things happen on each frame.

Page 38: HTML5 & JavaScript Games

PHYSICS UPDATEUpdating positions

The physics update is part of the game loop that deals with any kind of physics, or even just simple movement.

Page 39: HTML5 & JavaScript Games

GRAPHICS UPDATESMaking things visible

Graphics updates are another part of the game loop, but they deal with drawing all the game assets onto the screen.

Page 40: HTML5 & JavaScript Games

NETWORKINGBreaking out of single player

Network infrastructure will allow you to take your game multiplayer, perhaps even real-time by using WebSockets.

Page 41: HTML5 & JavaScript Games

CONTROLS AND UISo important to get right

Controls and UI, so massively important to get right.

For Rawkets I hired a designer to do the UI for me so it was as good as possible.

Page 42: HTML5 & JavaScript Games

Seb Lee-Delisle implemented some awesome JavaScript controls for iOS devices based on some previous iOS games.

http://sebleedelisle.com/2011/04/multi-touch-game-controller-in-javascripthtml5-for-ipad/http://www.gamasutra.com/view/feature/6323/a_guide_to_ios_twin_stick_shooter_.php

Page 43: HTML5 & JavaScript Games

PERFORMANCEKeeping things running smoothly

Performance isn’t really a fundamental feature, but it’s definitely something that you need to be keeping on top off through the development of a game with HTML5.

I’ll run through a couple of tips for performance in a moment.

Page 44: HTML5 & JavaScript Games

GAME ENGINESMaking game development easy

You don't have to create an entire game infrastructure from scratch…

Page 45: HTML5 & JavaScript Games

Impact.

I used this recently, and it’s really well made.

The documentation is great and the author is active and very helpful.

Will support WebSockets with a plugin, although you’ll still need to code the network logic from scratch.

http://impactjs.com/

Page 46: HTML5 & JavaScript Games

Crafty

Not my favourite engine, but it’s free and is doing much better than other free engines out there.

http://craftyjs.com/

Page 47: HTML5 & JavaScript Games

IsongenicEngine.

One of the most promising engines out there today.

Massively multiplayer networking built inUses Node and MongoDBCanvas or DOM-based graphics

http://www.isogenicengine.com

Page 48: HTML5 & JavaScript Games

ISOGENICENGINEIso City demo

Rob Evan’s, the creator of Isogenic, is putting together a little demo called Iso City to show off the engine.

Page 49: HTML5 & JavaScript Games

LIBRARIES & SERVICESSaving you time in the long run

If you do want to build your own engine, then there are some libraries and external services out there which might help you from having to create everything from scratch…

Page 50: HTML5 & JavaScript Games

BOX2DWEBJavaScript port of the Box2D physics engine

Box2DWeb allows you to add a physics engine into your game with little hassle.

It’s a port of the Box2D physics engine, which is the same one that Angry Birds uses.

http://code.google.com/p/box2dweb/

Page 51: HTML5 & JavaScript Games

MOTION.JSSolving client-side prediction issues

Motion.js is a new library that attempts to solve the client-side prediction issues with multiplayer gaming, some of the most difficult issues to overcome.

The library doesn’t do much yet, but it’s a great idea and something to keep an eye on.

https://github.com/tmpvar/motionjs

Page 52: HTML5 & JavaScript Games

Joyent’s Node cloud servers

Provides a remotely hosted Node server that allows you to quickly get a multiplayer game up and running.

It supports WebSockets, which a lot of other hosted Node services doesn’t allow.

Page 53: HTML5 & JavaScript Games

Pusher is for real-time communication and uses WebSockets.

Remotely hosted.

This is the service that Word² uses, the game that I highlighted earlier.

Page 54: HTML5 & JavaScript Games

Kaazing.

Self-hosted WebSockets communication platform.

http://kaazing.com/

Page 55: HTML5 & JavaScript Games

There’s also Electroserver5.

Self-hosted communication platform (not WebSockets) and game server for logic.

http://www.electrotank.com/es5.html

Page 56: HTML5 & JavaScript Games

KEY LESSONSWhat I’ve learnt so far

Over the last few years I’ve learnt a lot of lessons about game development.

Here are just a few of the most important ones.

Page 57: HTML5 & JavaScript Games

CUT DOWN LOOPSLess is more

The less loops you can get away with the faster your game will be.

Do as little as possible, no more.

Page 58: HTML5 & JavaScript Games

NO TIMEOUTSUse requestAnimationFrame instead

Another simple lesson is to stop using JavaScript timeouts and to use requestAnimationFrame instead.

requestAnimationFrame puts the browser in control of when an animation should happen and generally increases performance. It also doesn’t run in an inactive browser tab, saving you more precious resources.

Paul Irish has made a shim that allows you to use the requestAnimationFrame API in browsers that don’t support it. It won’t have the cool browser-controlled animations, but it means that you don’t need to change your code.

http://paulirish.com/2011/requestanimationframe-for-smart-animating/

Page 59: HTML5 & JavaScript Games

NETWORK TRICKSCutting down on communication

Be very aware about how much data you’re sending back and forth when creating a multiplayer game.

Page 60: HTML5 & JavaScript Games

1 1

MESSAGES IN

1MESSAGES OUT

1

Having only one player in the game is easy, you have one message coming in to the server, saying the player has moved, for example, and one message coming back out, updating the player with details from the server.

Page 61: HTML5 & JavaScript Games

1 2

2 1

MESSAGES IN

2MESSAGES OUT

4

So say we now have two players, there is still only 1 message in from each player, but now each player receives 2 messages back from the server; one for them, and one for the other player.

This isn’t too bad, but notice how the server is having to send 4 messages – 2 for each player.

Page 62: HTML5 & JavaScript Games

1 4

4 1

1

4

4

1

MESSAGES IN

4MESSAGES OUT

16

4 players now, look how the server is having to send 16 messages, yet it only receives 4.

If you haven’t already noticed, the messages out from the server are the square of the number of players.

But 16 messages out is alright, it’s hardly going to tax the server.

Page 63: HTML5 & JavaScript Games

MESSAGES IN

30MESSAGES OUT

9001 30

30 1

1

30

30

1

So imagine if we now move into properly multiplayer territory.

30 players in the game would mean 30 messages coming in to the server, and 900 – NINE HUNDRED – messages going out, every update. That’s a silly amount of data for just 30 people.

But let’s go further still…

Page 64: HTML5 & JavaScript Games

MESSAGES IN

100MESSAGES OUT

100001 100

100 1

1

100

100

1

Say we go massively multiplayer and have 100 players in the game at one time.

It’s not so bad for each individual player, they send one message in and get 100 back – that’s bearable.

But check out the server, it’s getting 100 messages in and is having to send out 10,000 back, every update. That’s just a mentally stupid number that’s going to cause a lot of grief.

Fortunately there is a way around this that cuts down the amount of messages sent from the server; you just need to send data only for players visible to another player, in essence filtering out game data that doesn't affect the current player. It’s such a simple solution, but it’s one that I never even considered at first.

Page 65: HTML5 & JavaScript Games

CHEATERSYou can’t avoid them completely

Your code is going to be visible to anyone who wants to look at the source.

It’s mainly an issue with multiplayer gaming, as cheating in a single player game would only affect one player.

I experienced this early on in the development of Rawkets. The solution is to make the server authoritative, which means that you prevent manipulation of the player’s code from doing any damage.

Page 66: HTML5 & JavaScript Games

DOM OR CANVASWhich one should you use?

How do you decide whether you go with the DOM or canvas for rendering?

This is something that I’ve been thinking about a lot lately.

In my opinion DOM has some performance benefits, but canvas is the right tool and is going in a better direction.

The situations reminds me a little of the way tables were used for layout before CSS was adopted. Using DOM content elements for animation feels hacky, in a bad way.

I don’t have an easy solution that will make canvas perform as well, but I think it’s important that we try and make and use canvas better rather than turning our backs on it and clinging onto the DOM.

Now I'm being a bit dramatic, but I really think developers need to think long and hard before turning away from the ideal technologies like JavaScript, canvas and SVG.

This is something that I hope to look into a lot more in the near future.

Page 67: HTML5 & JavaScript Games

THE FUTUREWhat HTML5 gaming still needs

HTML5 gaming is still at an early stage and there are still plenty of things that can be improved.

Page 68: HTML5 & JavaScript Games

DOCUMENTATIONSpecific to HTML5 game development

More documentation and teaching materials on the complex areas of game development in JavaScript.

We need something like Glen Fiedler’s Gaffer on Games website, but dedicated to HTML5 instead.

Page 69: HTML5 & JavaScript Games

WEBGL SUPPORTSupport is shocking right now

Wider support for WebGL.

Page 70: HTML5 & JavaScript Games

MORE ENGINESNot many decent ones around right now

Better game engines that really solve all the major issues, particularly with multiplayer games.

Page 71: HTML5 & JavaScript Games

IMPROVED AUDIOMore reliable looping

Improved HTML5 audio support, with proper looping.

Page 72: HTML5 & JavaScript Games

POWERFUL MOBILEBetter hardware acceleration

More powerful mobile devices, with better hardware acceleration for canvas.

Page 73: HTML5 & JavaScript Games

BETTER CONTROLSBreak away from the keyboard and mouse

Better more console-like systems for control, like Seb Lee-Delisles iOS controller.

Page 74: HTML5 & JavaScript Games

Brass Monkey turns a smart phone into a controller, which I had the luxury of seeing demoed recently and I must say that it’s pretty damn cool.

http://brassmonkeysdk.com/

Page 75: HTML5 & JavaScript Games

MONETISATIONMaking profit from games

Better monetisation options for developers.

Right now there aren't (m)any games making a profit out of these new technologies.

How do you monetise a game where the code is open for all to see?

Page 76: HTML5 & JavaScript Games

Rawkets.comHTML5 & WebSockets game.

Twitter sentiment analysisDelving into your soul.

RECENT PROJECTS

Rawkes.comPersonal website and blog

MORE COOL STUFF

ExplicitWeb.co.ukWeb development podcast.

Mozilla Technical EvangelistMy job

ROB HAWKES@robhawkes

Twitter - @robhawkesRawkes - http://rawkes.com

Page 77: HTML5 & JavaScript Games

Become a canvas master

FOUNDATION HTML5 CANVASMy amazing book on canvas, animation, and making games.

RAWKES.COM/FOUNDATIONCANVAS

Out now

Paperback and digital formats

Learn how to animate

Make two cool space games

Book - available on Amazon right now

Page 78: HTML5 & JavaScript Games

COFFEE FWDMozilla Web FWD get together

Innovation acceleration

FRI JULY 15TH @ RED ROCK CAFE, MV

Friday July 15th

Red Rock Cafe, Mountain View

Meet some great people

Show off your stuff

Just a quick one to let you know about an event Mozilla are putting on this Friday in Mountain View.

It’s called Coffee FWD and it’s a get together related to the Web FWD Innovation Accelerator for the open Web.

It’s a chance for us to work with you to help push the Web forward, so if you’re working on something cool then you should definitely come down and say hi.

http://coffeefwd.eventbrite.com/https://www.webfwd.org

Page 79: HTML5 & JavaScript Games

DEV DERBYExperimenting with the latest Web technologies

Manipulate video with canvas

DEVELOPER.MOZILLA.ORG/EN-US/DEMOS/DEVDERBY

Every month

This month is HTML5 video

Win prizes (like an Android)

Next month is all about touch

Also, you definitely take part in the Dev Derby, which is a monthly competition run by the Mozilla Developer Network to see what can be done with the latest Web technologies.

This month the focus is on HTML5 video, which is pretty interesting considering that you can manipulate it using the canvas.

The winners get cool prizes, like an Android phone. It’s a great excuse to play around with these technologies.

https://developer.mozilla.org/en-US/demos/devderby

Page 80: HTML5 & JavaScript Games

Thanks for listening, and I hope you enjoy the rest of your day.

I believe we have some time set aside for questions…