/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
* Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Oracle Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* GameData.fx
*
*/
package sl;
import javafx.animation.transition.*;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import sl.Board;
import sl.Main;
import sl.Player;
/**
* @author JavaFX Samples Team
*/
var dim = bind Board.dim;
var sqSide = bind Board.sqSide;
var maxturn = 2 - 1 ;//this is for max players - 1- currently only 2 players
// class GameData - has game information
public class GameData {
public var turn = 0;
public var prevturn = 1;
//this contains snakes and ladders information
public var moves:Integer[] = [
0,
1,2,3,4,5,6,7,8,9,10,
11,12,13,14,/*15*/48,16,/*17*/7,18,19,20,
21,22,/*23*/44,24,25,26,27,28,29,/*30*/89,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,/*56*/83,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,
71,72,73,74,75,76,77,78,79,/*80*/39,
81,82,83,84,/*85*/46,86,87,88,89,90,
91,92,93,94,95,96,/*97*/25,98,99,100
];
//updateGame this is the crux of the game logic
public function updateGame(diceval:Integer):Void{
Main.panel.previousdiceValue = diceval;
var pathTransition:PathTransition;
var seqTransition:SequentialTransition;
var newpos:Integer = Main.bd.players[turn].currentsquare + diceval;
//check if this new move is possible
if (newpos <= 100){
//check if this is a win
if (newpos == 100){
showMessage("Game Over.\nPlayer {turn + 1} wins");
for (p:Player in Main.bd.players){
p.currentsquare = 0;
}//end for
}//end if for checking win
else {
//not win yet
//going to new position as indicated by dice
Main.bd.players[turn].currentsquare = newpos;
//if there is snake or ladder here show the animation
if (Main.bd.blocks[newpos].start == true){
var oldpos = newpos;
newpos = Main.bd.blocks[oldpos].stop;
Main.bd.players[turn].currentsquare = newpos;
changeTurn();
//DICE CODE
/* var path = Path {
elements: [
MoveTo {
x: Main.bd.blocks[oldpos].xPos + sqSide / 2
y: Main.bd.blocks[oldpos].yPos + sqSide / 2
},
LineTo {
x: Main.bd.blocks[newpos].xPos + sqSide / 2
y: Main.bd.blocks[newpos].yPos + sqSide / 2
}
]
};
pathTransition = PathTransition {
duration: 6s
node: Main.bd.players[turn]
path: AnimationPath.createFromPath(path)
orientation: OrientationType.NONE//OrientationType.ORTHOGONAL_TO_TANGENT
repeatCount: 1
action: function(){
//first set the current player to correct position
//and then change the turn
//Main.bd.players[turn].currentsquare = newpos;
//diceval = 0;
changeTurn();
//Main.panel.dice.disable = false;
Main.panel.dice.visible = true;
}//end action function
};
// pathTransition.play();
*/
}//end if show snake and ladder
else {
//if there is not snake or ladder to animate then
//immediately show the dice
changeTurn();
//DICE CODE
}//end else
}//end if else for win
}//end if newpos <=100
else {
//TODO
//showMessage("Player {turn +1 }\nYou need {100 - Main.bd.players[turn].currentsquare} or less \nto move further");
changeTurn();
//DICE CODE
}//end else
}//end function updateGame
//show message on screen
//requires a mouse click to get rid of the message
public function showMessage(s:String){
//DICE CODE
var winMessage:Group = Group{
content: [
Rectangle {
x: dim / 5,
y: dim / 4
width: dim * 3/ 5 ,
height: dim / 2
fill: Color.BLACK
blocksMouse: true
onMouseClicked:function(e:MouseEvent):Void {
delete winMessage from Main.bd.g.content;
//show the dice again
//DICE CODE
}//end mouse click
},
Text {
font:Font {
size: sqSide - 10
}
x: dim / 4 + 2
y: dim / 4 + sqSide
fill: Color.LIGHTCORAL
content: s
}
]
};
insert winMessage into Main.bd.g.content;
}//end show Message
function changeTurn(){
if (turn ==maxturn){
turn =0;
prevturn = maxturn;
}
else {
turn = turn + 1;
prevturn = turn - 1;
}
}//end change turn
}//end class GameData