----------------- Part 5.1 ----------------- Code Sample 5.1.1 " function SceneWindow2D::onMouseMove(%this, %modifier, %worldPos, %mouseClicks) { // set the position of the object that is designated to follow the mouse $mouseObj.setPosition(%worldPos); } function SceneWindow2D::onMouseDragged(%this, %modifier, %worldPos, %mouseClicks) { // set the position of the object that is designated to follow the mouse $mouseObj.setPosition(%worldPos); } " Code Sample 5.1.2 " function SceneWindow2D::onMouseUp(%this, %modifier, %worldPos, %mouseClicks) { // if the clientcheckerboard is created then pick the tile if(ClientCheckerBoard.boardLayer !$= "") %tile = ClientCheckerBoard.boardLayer.pickTile(%worldPos); // if tile has a value then attempt to select it if(%tile !$= "") attemptSelectChecker(getWord(%tile, 0), getWord(%tile, 1)); } " Code Sample 5.1.3 " function attemptSelectChecker(%x, %y) { // first we check if it is fact our turn if(!$myTurn) { } else if($clientSelected) { // this means we already have a checker selected, so lets call our // attempt to move function attemptMovePiece(%x, %y); } else { // grab the selected piece %selection = ClientCheckerBoard.getPiece(%x, %y); // get the possible king piece for that team %king = getKing($playerTeam); // if it's of the right team then let's attempt to check it if(%selection == $playerTeam || %selection == %king) { // ask the server if we can select it commandToServer('attemptSelectChecker', %x, %y); } else { // let the console know this is an invalid selection echo("invalid selection"); } } } " Code Sample 5.1.4 " function serverCmdAttemptSelectChecker(%client, %x, %y) { // pass the attempted selection values on to the server function serverAttemptSelectChecker(%client, %x, %y); } " Code Sample 5.1.5 " function serverAttemptSelectChecker(%client, %x, %y) { // check if it's this client's turn if($playersTurn != %client) { // it is not this cient's turn, clever client, the server is all knowing commandToClient(%client, 'notYourTurn'); } else if($playersTurn == %client) { // grab the selection %selection = ServerCheckerBoard.getPiece(%x, %y); // grab the possible king %king = getKing(%client.team); // check if this is the client's piece, we also check if // it's on of their kings if(%selection == %client.team || %selection == %king) { // proper move, proces sit serverSelectChecker(%client, %x, %y); } else { // the client cannot move another team's piece, bad client commandToClient(%client, 'selectCheckerResponse', %x, %y, $no); } } } " Code Sample 5.1.6 " function serverSelectChecker(%client, %x, %y) { // set the selected piece $playersTurn.selectedX = %x; $playersTurn.selectedY = %y; $playersTurn.hasSelected = true; // respond to the client commandToClient(%client, 'selectCheckerResponse', %x, %y, $yes); } " Code Sample 5.1.7 " function clientCmdNotYourTurn() { // tell the client that you -cannot- play when its NOT your turn ;) MessageBoxOK("Not your turn...", "It is currently not your turn", ""); } function clientCmdSelectCheckerResponse(%x, %y, %answer) { // here is our select response, if its a no we tell the client that you cannot // select the piece, if its a yes then we select the piece if(!%answer) { MessageBoxOK("Invalid Selection...", "You cannot select this checker piece!", ""); } else { clientSelectChecker(%x, %y); } } " Code Sample 5.1.8 " function clientSelectChecker(%x, %y) { // set the selection with what's passed in $clientSelectedX = %x; $clientSelectedY = %y; $clientSelected = true; // call the mount function to mount the checker to the mouse ClientCheckerBoard.mountCheckerToMouse(%x, %y); } " Code Sample 5.1.9 " function ClientCheckerBoard::mountCheckerToMouse(%this, %x, %y) { // grab the checker %checker = %this.images[%x, %y]; // store the selected checker $clientSelectedPiece = %checker; // change the layers of the checker and its eyes so we can see // it over other pieces, while mounted %checker.setLayer(0); // mount the piece to the mouse %checker.mount($mouseObj); } "