infosoul/infosoul.coffee

227 lines
7.7 KiB
CoffeeScript

globalBoard = ""
globalMyPos = []
globalMyPos[0] = 15
globalMyPos[1] = 5
gGoalPos = []
gGoalPos[0] = 0
gGoalPos[1] = 0
godmode = false
started = false
dead = false
p1Texture = "me"
goalTexture = "me"
createDisplayText = () ->
tmp = "
ButImustexplaintoyouhowallthismistakenideaofdenouncingpleasu" + "\n" + "
reandpraisingpainwasbornandIwillgiveyouacompleteaccountofthe" + "\n" + "
system,andexpoundtheactualteachingsofthegreatexplorerofthetr" + "\n" + "
uth,themasterbuilderofhumanhappiness.Noonerejects,dislikes,o" + "\n" + "
ravoidspleasureitself,becauseitispleasure,butbecausethosewho" + "\n" + "
donotknowhowtopursuepleasurerationallyencounterconsequencest" + "\n" + "
hatareextremelypainful.Noragainisthereanyonewholovesorpursue" + "\n" + "
sordesirestoobtainpainofitself,becauseitispain,butbecauseocc" + "\n" + "
asionallycircumstancesoccurinwhichtoilandpaincanprocurehimso" + "\n" + "
megreatpleasure.Totakeatrivialexample,whichofuseverundertake" + "\n" + "
slaboriousphysicalexercise,excepttoobtainsomeadvantagefromit" + "\n" + "
Butwhohasanyrighttofindfaultwithamanwhochoosestoenjoyapleasu" + "\n" + "
rethathasnoannoyingconsequences,oronewhoavoidsapainthatprodu" + "\n" + "
cesnoresultantplea.pleasurerationally.encounterconsequencest"
tmp.toUpperCase()
createStartText = () ->
tmp = "
ButImustexplaintoyouhowallthismistakenideaofdenouncingpleasu" + "\n" + "
reandpraisingpainwasbornandIwillgiveyouacompleteaccountofthe" + "\n" + "
syst________________________achingsofthegreatexplorerofthetr" + "\n" + "
uth,__use the arrowkeys_____appiness.Noonerejects,dislikes,o" + "\n" + "
ravo________________________eitispleasure,butbecausethosewho" + "\n" + "
dono__find yourself_________rationallyencounterconsequencest" + "\n" + "
hata________________________ainisthereanyonewholovesorpursue" + "\n" + "
sord__press 1 to start______lf,becauseitispain,butbecauseocc" + "\n" + "
asio________________________nwhichtoilandpaincanprocurehimso" + "\n" + "
megreatpleasure.Totakeatrivialexample,whichofuseverundertake" + "\n" + "
slaboriousphysicalexercise,excepttoobtainsomeadvantagefromit" + "\n" + "
Butwhohasanyrighttofindfaultwithamanwhochoosestoenjoyapleasu" + "\n" + "
rethathasnoannoyingconsequences,oronewhoavoidsapainthatprodu" + "\n" + "
cesnoresultantplea.pleasurerationally.encounterconsequencest"
tmp.toLowerCase()
createWinPicture = () ->
tmp = "
____________________________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
__Congratulations___________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
__You Won!__________________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
__Thank you for playing,____________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
____________________________________________________________" + "\n" + "
__________________________________________nils@thunki.com___" + "\n" + "
____________________________________________________________"
tmp
createLosePicture = () ->
tmp = "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXX________________________XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXX__Lost in interwebs x_X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXX________________________XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXX__Press 1 to restart____XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXX________________________XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "\n" + "
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
tmp
makeWinState = () ->
globalBoard = createWinPicture()
godmode = true
p1Texture = "ME"
goalTexture = "__"
gGoalPos[0] = 0
gGoalPos[1] = 0
setMyPos(globalMyPos[0], globalMyPos[1])
makeLoseState = () ->
globalBoard = createLosePicture()
p1Texture = "XX"
goalTexture = "XX"
dead = true
started = false
drawBoard(globalBoard,440)
checkWin = () ->
test1x = globalMyPos[0]
test2x = gGoalPos[0]
test1y = globalMyPos[1]
test2y = gGoalPos[1]
#alert "To be tested: " + test1 + " " + test2
if test1x is test2x and test1y is test2y
makeWinState()
setMyPos = (x, y) ->
globalMyPos[0] = x
globalMyPos[1] = y
tmpString = "<p id=\"currentPos\">Current position: " + x + ", " + y + "</p>"
$('#currentPos').replaceWith(tmpString)
checkWin()
newPos = x + y*62
drawBoard(globalBoard,newPos)
drawBoard = (InpGbBoard, strPos) ->
#Insert player
leftStr = InpGbBoard[0..strPos--]
rightStr = InpGbBoard[strPos+4..InpGbBoard.length]
newBoard = leftStr +
p1Texture + rightStr
#Insert goal, but first make sure the goal is placed right.
newGoalPos = gGoalPos[0] + gGoalPos[1]*62
leftStr = newBoard[0..newGoalPos--]
rightStr = newBoard[newGoalPos+4..newBoard.length]
newBoard = "<div id=\"gamediv\">" + leftStr +
goalTexture + rightStr + "</div>"
$('#gamediv').replaceWith(newBoard)
tmpString = "<p id=\"winPos\">Where to I need to get to: " + gGoalPos[0] + ", " + gGoalPos[1] + "</p>"
$('#winPos').replaceWith(tmpString)
init = () ->
globalBoard = createStartText()
godmode = false
p1Texture = "me"
goalTexture = "me"
newBoard = "<div id=\"gamediv\">" + globalBoard + "</div>"
$('#gamediv').replaceWith(newBoard)
$(document).ready ->
init()
#up-right-down-left -> 38, 39, 40, 37
document.onkeydown = (event)->
switch event.keyCode
when 37
$('#keydebug').replaceWith("<p id=\"keydebug\">Key pressed: Left</p>")
if started
globalMyPos[0]--
setMyPos(globalMyPos[0], globalMyPos[1])
makeMonster()
checkMonster()
when 38
$('#keydebug').replaceWith("<p id=\"keydebug\">Key pressed: Up</p>")
if started
globalMyPos[1]--
setMyPos(globalMyPos[0], globalMyPos[1])
makeMonster()
checkMonster()
when 39
$('#keydebug').replaceWith("<p id=\"keydebug\">Key pressed: Right</p>")
if started
globalMyPos[0]++
setMyPos(globalMyPos[0], globalMyPos[1])
makeMonster()
checkMonster()
when 40
$('#keydebug').replaceWith("<p id=\"keydebug\">Key pressed: Down</p>")
if started
globalMyPos[1]++
setMyPos(globalMyPos[0], globalMyPos[1])
makeMonster()
checkMonster()
when 49
if dead
init()
dead = false
started = false
if not started
started = true
globalBoard = createDisplayText()
gGoalPos[0] = Math.floor(Math.random() * 50)
gGoalPos[1] = Math.floor(Math.random() * 11)
setMyPos(globalMyPos[0], globalMyPos[1])
makeMonster = () ->
#Randomly place monster on map
monsterLoc = Math.floor(Math.random() * 780)
#alert monsterLoc
leftStr = globalBoard[0..monsterLoc-1]
rightStr = globalBoard[monsterLoc+1..globalBoard.length-1]
globalBoard = leftStr +
"*" + rightStr
checkMonster =() ->
#check if you made contact with a monster
#what's underneath player?
meLoc = globalMyPos[0] + globalMyPos[1]*62
underLeftFoot = globalBoard[meLoc+1..meLoc+1]
underRightFoot = globalBoard[meLoc+2..meLoc+2]
monster = "*"
if underLeftFoot is monster or underRightFoot is monster
if not godmode
started = false
makeLoseState()