Skip to content

Commit

Permalink
display stars in level summary and save to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Urbas committed Nov 30, 2013
1 parent dae8d09 commit 2b15aff
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 21 deletions.
20 changes: 18 additions & 2 deletions app/data/others.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,28 @@
"tileh": 55,
"frames": 1
},
"ui_level_progress": {
"ui_level_progress": {
"sx": 0,
"sy": 0,
"sy": 353,
"cols": 1,
"tilew": 30,
"tileh": 30,
"frames": 1
},
"ui_level_score": {
"sx": 0,
"sy": 350,
"cols": 1,
"tilew": 79,
"tileh": 90,
"frames": 1
},
"ui_level_score_empty": {
"sx": 79,
"sy": 350,
"cols": 1,
"tilew": 79,
"tileh": 90,
"frames": 1
}
}
79 changes: 69 additions & 10 deletions app/scripts/game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app/scripts/game/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ window.Game =
Q.enableSound()

# game progress
Game.storageKey = "zombieGame:availableLevel"
Game.availableLevel = localStorage.getItem(Game.storageKey) || 1
Game.storageKeys =
availableLevel: "zombieGame:availableLevel"
levelProgress: "zombieGame:levelProgress"
Game.availableLevel = localStorage.getItem(Game.storageKeys.availableLevel) || 1

# used for collision detection
@SPRITE_NONE = 0
Expand Down
60 changes: 53 additions & 7 deletions app/scripts/game/scenes/level_summary.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ Q.scene "levelSummary", (stage) ->
# some math
marginY = Q.height * 0.25

marginXinP = 20 # %
gutterXinP = 8 # %
columnsNo = 2

# layout math
columnInP = (100 - (marginXinP * 2) - (columnsNo - 1) * gutterXinP)/columnsNo # 24%

marginX = Q.width * marginXinP * 0.01
gutterX = Q.width * gutterXinP * 0.01
columnWidth = Q.width * columnInP * 0.01

# audio
Q.AudioManager.stopAll()

Expand All @@ -18,14 +29,14 @@ Q.scene "levelSummary", (stage) ->
size: 100

# add level summary
container = stage.insert new Q.UI.Container
x: Q.width/2
summaryContainer = stage.insert new Q.UI.Container
x: marginX + columnWidth/2
y: Q.height/2

lineHeight = 50

if stage.options.health
container.insert new Q.UI.Text
summaryContainer.insert new Q.UI.Text
x: 0
y: -lineHeight * 2
label: "Health collected: " + stage.options.health.collected + "/" + stage.options.health.available
Expand All @@ -34,7 +45,7 @@ Q.scene "levelSummary", (stage) ->
size: 36

if stage.options.zombies
container.insert new Q.UI.Text
summaryContainer.insert new Q.UI.Text
x: 0
y: -lineHeight
label: "Zombies healed: " + stage.options.zombies.healed + "/" + stage.options.zombies.available
Expand All @@ -43,7 +54,7 @@ Q.scene "levelSummary", (stage) ->
size: 36

if stage.options.bullets
container.insert new Q.UI.Text
summaryContainer.insert new Q.UI.Text
x: 0
y: 0
label: "Bullets waisted: " + stage.options.bullets.waisted + "/" + stage.options.bullets.available
Expand All @@ -52,7 +63,7 @@ Q.scene "levelSummary", (stage) ->
size: 36

if stage.options.zombieModeFound?
container.insert new Q.UI.Text
summaryContainer.insert new Q.UI.Text
x: 0
y: lineHeight
label: "Zombie Mode: " + if stage.options.zombieModeFound then "done" else "not found"
Expand Down Expand Up @@ -104,4 +115,39 @@ Q.scene "levelSummary", (stage) ->
# save progress in game
if Q.state.get("currentLevel") >= Game.availableLevel
Game.availableLevel = Q.state.get("currentLevel") + 1
localStorage.setItem(Game.storageKey, Game.availableLevel)
localStorage.setItem(Game.storageKeys.availableLevel, Game.availableLevel)


# count stars
score = stage.options.zombies.healed/stage.options.zombies.available
stars = 0

if score <= 0.5
stars = 1
else if score > 0.5 && score < 0.9
stars = 2
else
stars = 3

# save only if better than previous
previousStars = localStorage.getItem(Game.storageKeys.levelProgress + ":" + Q.state.get("currentLevel"))
if previousStars < stars
localStorage.setItem(Game.storageKeys.levelProgress + ":" + Q.state.get("currentLevel"), stars)

# insert stars on the screen
starsContainer = stage.insert new Q.UI.Container
x: summaryContainer.p.x + gutterX + columnWidth
y: Q.height/2

x = -80 - 20 # width of LevelScoreImg - margin between stars

for index in [1..3]
empty = if stars >= index then false else true

scoreImg = starsContainer.insert new Q.UI.LevelScoreImg
x: x
y: -lineHeight/2
empty: empty

x += scoreImg.p.w + 20

12 changes: 12 additions & 0 deletions app/scripts/game/sprites/ui/level_score_img.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Q = Game.Q

Q.UI.LevelScoreImg = Q.Sprite.extend "Q.UI.LevelScoreImg",
init: (p) ->
@_super p,
x: 0
y: 0
sheet: "ui_level_score"

if @p.empty
@p.sheet = "ui_level_score_empty"

0 comments on commit 2b15aff

Please sign in to comment.