From 220d246b42110e945eb24b8f200913ee74859ef8 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Wed, 1 Jan 2014 20:30:20 +0000 Subject: [PATCH] added subsection on keeping score for all players --- ...YoungPersonsGuideToProgrammingMinecraft.md | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/YoungPersonsGuideToProgrammingMinecraft.md b/docs/YoungPersonsGuideToProgrammingMinecraft.md index a396f77..6f547be 100644 --- a/docs/YoungPersonsGuideToProgrammingMinecraft.md +++ b/docs/YoungPersonsGuideToProgrammingMinecraft.md @@ -33,6 +33,7 @@ * [Event-Driven programming](#event-driven-programming) * [Stop listening to events](#stop-listening-to-events) * [Keeping Score - Lookup tables in Javascript](#keeping-score---lookup-tables-in-javascript) + * [Counting block break events for each player](#counting-block-break-events-for-each-player) * [Next Steps](#next-steps) ## Introduction @@ -1102,7 +1103,7 @@ even have to be a string literal - it can be a variable like this... ... in the above example, the propertyName variable is used when indexing. What this means is that every object in JavaScript can act -like a lookup table? What's a lookup table? A table you 'look up' of +like a lookup table. What's a lookup table? A table you 'look up' of course. This is a table of names and scores... Name Score @@ -1159,6 +1160,32 @@ look something like this... return scores[name]; }; +## Counting block break events for each player + +I can use a Javascript lookup table (a plain old Javascript object) to +keep a count of how many blocks each player has broken ... + +#### block-break-counter.js + + var breaks = {}; + // every time a player joins the game reset their block-break-count to 0 + events.on('player.PlayerJoinEvent', function(listener, event){ + breaks[event.player] = 0; + }); + events.on('block.BlockBreakEvent', function(listener, event){ + var breaker = event.player; + var breakCount = breaks[breaker.name]; + breakCount++; // increment the count. + breaks[breaker.name] = breakCount; + + breaker.sendMessage('You broke ' + breakCount + ' blocks'); + + }); + +With a little more work, you could turn this into a game where players +compete against each other to break as many blocks as possible within +a given time period. + ## Next Steps This guide is meant as a gentle introduction to programming and