acrossTheRiver.html

<HTML>

<HEAD>

<script>var dFrameFilePath = '../../../'</script>

<script language="Javascript" src="../../../lib/dFrameAPI.js"></script>

<script language="Javascript">

//DynAPI libraries

DynAPI.setLibraryPath('../../../dynapi/src/lib/');

DynAPI.include('dynapi.util.thread.js');

DynAPI.include('dynapi.util.pathanim.js');

//Style

var dFrameStyleForText

DFrameAPI.include('./acrossTheRiver/acrossTheRiverStyle.js', 'A')

var leftSide, river, rightSide

var wolf, goat, cabbage

var boat

var barInsertCoins

var travelNb

DFrameAPI.onLoad = function(){

var baseAll = new DFrame([0, 0, 100, 100], 'This is the very amazing gane "Across the river" ...', dFrameStyle)

dFrameStyle.setTitleBar(false)

var dFrameStyleForSide = new DFrameStyle(dFrameStyle)

dFrameStyleForSide.getDefaultBarStyle().setStartFrom('TOP')

dFrameStyleForSide.getDefaultBarStyle().setAutoHeight('CONTENT')

leftSide = new DFrame([0, 25, '90', 75], 'Left side', dFrameStyleForSide, baseAll)

leftSide.setBackgroundColor('green')

position = [0, 25, 90, '118']

position[0] = [leftSide, 'Right', 0];

river = new DFrame(position, 'River', dFrameStyle, baseAll)

river.setBgImage('./acrossTheRiver/river.gif')

barInsertCoins = river.addBar(['*','*','*','*'])

barInsertCoins.addButton('Insert coins', 'inits()')

position = [0, 25, '90', 75]

position[0] = [river, 'Right', 0];

rightSide = new DFrame(position, 'Right side', dFrameStyleForSide, baseAll)

rightSide.setBackgroundColor('green')

leftSide.show()

river.show()

rightSide.show()

}

function inits() {

barInsertCoins.show(false)

if (!boat) boat = new Boat()

travelNb = -1

if (boat.counter) boat.counter.show(true)

if (boat.position == 'rightSide') boat.travel()

travelNb = 0

boat.init()

if (!wolf) wolf = new Passenger('wolf', 'Yap ! Yap ! !')

wolf.init()

if (!goat) goat = new Passenger('goat', 'Meuuuuuuhhh !')

goat.init()

if (!cabbage) cabbage = new Passenger('cabbage', 'Crunch ! Crunch !')

cabbage.init()

}

function hide() {

boat.bar.show(false)

boat.counter.show(false)

wolf.hide()

goat.hide()

cabbage.hide()

barInsertCoins.show(true)

}

function Passenger(name, shout) {

this.name = name

this.shout = shout

//"this" in action of a Button: Indicates the Button. Here: this.leftSide

//The transfert method is a method the Passenger class

//Set this.leftSide.passenger and Use "this.passenger" as target and set it as the owner of the Button

this.leftSide = leftSide.addButtonImage(name, 'this.passenger.transfert()')

this.leftSide.passenger = this

this.boat = boat.bar.addButtonImage(name, 'this.passenger.transfert()')

this.boat.passenger = this

this.rightSide = rightSide.addButtonImage(name, 'this.passenger.transfert()')

this.rightSide.passenger = this

this.init()

}

Passenger.prototype.init = function () {

this.leftSide.show(true)

this.boat.show(false)

this.rightSide.show(false)

this.position = 'leftSide'

}

Passenger.prototype.hide = function () {

this.init()

this.leftSide.show(false)

}

Passenger.prototype.transfert = function () {

if (this.position != 'boat' && (this.position != boat.position)) {

this.afraidShout()

return

}

if (this.position == 'leftSide' || this.position == 'rightSide') {

this.leftSide.show(false)

this.rightSide.show(false)

this.boat.show(true)

this.position = 'boat'

boat.passenger = this

message('Vroum ! Vroum !', 0.5)

setTimeout('boat.travel()',1000)

} else {

this.boat.show(false)

if (boat.position == 'leftSide') {

this.leftSide.show(true)

boat.bar.setBgImage('./acrossTheRiver/boat.gif')

} else {

this.rightSide.show(true)

boat.bar.setBgImage('./acrossTheRiver/boatBack.gif')

}

this.position = boat.position

boat.passenger = null

message('Et hop !', 0.5)

}

checkOver()

}

Passenger.prototype.afraidShout = function () {

message('Plouf !<br>'+this.shout, 2)

}

function Boat() {

var style = river.getStyle().getDefaultBarStyle()

style.setTitleBar(false)

style.setBackgroundImage('./acrossTheRiver/boat.gif', 'A')

style.setAutoWidth(false)

style.setAutoHeight(false)

style.setItemsVAlign('Bottom')

this.bar = river.addBar('Boat',[0,0,'250', '120'], style)

this.bar.boat = this

var listener = new EventListener();

listener.onpathstop = function(e) {

if (boat.passenger != null) {

setTimeout('boat.passenger.transfert()', 1000)

}

}

this.bar.addEventListener(listener);

this.bar.addButton('Travel', 'boat.travel()')

this.init()

}

Boat.prototype.init = function () {

this.bar.show(true)

this.position = 'leftSide'

}

Boat.prototype.travel = function () {

if (this.position == 'leftSide') {

this.bar.setBgImage('./acrossTheRiver/boat.gif')

this.position = 'rightSide'

endPos = this.bar.parent.getWidth() - this.bar.getWidth()

} else {

this.bar.setBgImage('./acrossTheRiver/boatBack.gif')

this.position = 'leftSide'

endPos = 0

}

this.bar.slideTo(endPos, this.bar.getY())

if (!this.counter) this.counter = river.addText('', buttonStyleForText, [0, '150'])

travelNb += 1

var txt = travelNb > 1 ? ' travels' : ' travel'

this.counter.setText('<h2>' + travelNb + txt + '</h2>')

checkPositions()

}

function message(text, timer) {

river.addText('<H1>'+text+'</H1>', buttonStyleForText, ['*', '50'], timer)

}

function checkPositions() {

var end = false

if (boat.position != wolf.position && wolf.position == goat.position) {

message('Miam !Miam !<br>The wolf (yes, it is a wolf !) eat the cow:<br>You LOOSE !!<br>Game over.', 4)

end = true

} else {

if (boat.position != goat.position && goat.position == cabbage.position) {

message('Miam !Miam !<br>The cow eat the flower:<br> You LOOSE !!<br>Game over.', 4)

end = true

}

}

if (end) setTimeout('hide()', 4000)

}

function checkOver() {

var end = false

if (wolf.position == 'rightSide' && goat.position == 'rightSide' && cabbage.position == 'rightSide') {

message('<br>You WIN !!<br>Game over.', 3)

end = true

}

if (end) setTimeout('hide()', 4000)

}

</script>

</HEAD>