Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created July 13, 2011 11:53
Show Gist options
  • Save Sephi-Chan/1080170 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/1080170 to your computer and use it in GitHub Desktop.
Impact game
ig
.module('game.main')
.requires(
'impact.game'
'impact.font'
'game.menu'
'game.levels.test'
'game.entities.player'
'game.entities.levelchange'
'game.entities.trigger'
)
.defines ->
SFH = ig.Game.extend
# Load a font.
font: new ig.Font 'media/04b03.font.png'
gravity: 400
currentState: null
currentLevel: null
STATE:
MAIN_MENU: 0
PLAYING: 1
init: ->
@currentState = @STATE.MAIN_MENU
@cursorWidth = @font.widthForString(">>")
# Initialize your game here bind keys etc.
ig.input.bind ig.KEY.LEFT_ARROW, 'left'
ig.input.bind ig.KEY.RIGHT_ARROW, 'right'
ig.input.bind ig.KEY.UP_ARROW, 'up'
ig.input.bind ig.KEY.DOWN_ARROW, 'down'
ig.input.bind ig.KEY.X, 'jump'
ig.input.bind ig.KEY.SPACE, 'interact'
@menu = new Menu @font, [
new MenuItem "Start", @startGame
new MenuItem "Options", @showOptions
new MenuItem "Help", @showHelp
new MenuItem "Credits", @showCredits
]
update: ->
switch @currentState
when @STATE.MAIN_MENU
1
when @STATE.PLAYING
@currentLevel = LevelTest
@loadLevel(@currentLevel)
# Screen follows the player
player = this.getEntitiesByType(EntityPlayer)[0]
if player
@screen.x = player.pos.x - ig.system.width/2
@screen.y = player.pos.y - ig.system.height/2
@parent()
draw: ->
@parent()
switch @currentState
when @STATE.MAIN_MENU
baseX = ig.system.width / 2
text = "Startups from Hell"
@font.draw(text, baseX, 12, ig.Font.ALIGN.CENTER)
@menu.draw(baseX, 50)
text = "Make a choice with UP and DOWN then hit SPACE to confirm."
@font.draw(text, baseX, 140, ig.Font.ALIGN.CENTER)
startGame: ->
# @currentState = @STATE.PLAYING
console.log "Start game..."
showCredits: ->
console.log "Show credits..."
showOptions: ->
console.log "Show options..."
showHelp: ->
console.log "Show help..."
fps = 20
width = 320
height = 240
ratio = 1
ig.main '#canvas', SFH, fps, width, height, ratio
ig
.module('game.menu')
.defines ->
window.Menu = ig.Class.extend
init: (@font, @choices)->
@selectedChoice = 0
@cursorLeft = ">>"
@cursorLeftWidth = @font.widthForString(@cursorLeft)
@cursorRight = "<<"
@cursorRightWidth = @font.widthForString(@cursorRight)
# Calculate the label length only once.
for choice in @choices
choice.labelWidth = @font.widthForString(choice.label)
# Draw the menu begining at baseY in height.
# The menu is centered around the baseX axis.
draw: (baseX, baseY)->
for choice, i in @choices
y = baseY + i * 20
@font.draw(choice.label, baseX, y, ig.Font.ALIGN.CENTER)
if @selectedChoice == i
x = baseX - choice.labelWidth / 2 - @cursorLeftWidth - 8
@font.draw(@cursorLeft, x, y - 1)
x = baseX + choice.labelWidth / 2 + 8
@font.draw(@cursorRight, x, y - 1)
if ig.input.pressed('up')
@selectChoice(-1)
else if ig.input.pressed('down')
@selectChoice(+1)
else if ig.input.pressed('interact')
@choices[@selectedChoice].interact()
# Change the menu choice in the given shift:
# +1 for bottom, -1 for the top.
selectChoice: (shift)->
index = @selectedChoice + shift
if index < 0
@selectedChoice = @choices.length - 1
else
@selectedChoice = index % @choices.length
# A menu element that have a text label and a callback
# triggered when the item hit.
window.MenuItem = ig.Class.extend
init: (@label, @action)->
interact: ->
@action()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment