GameC
How do I use Game C
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: NetworkingPeer.cs
Copy
1631 public void OnEvent(EventData photonEvent)
1632 {
1633 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
1634 Debug.Log(string.Format("OnEvent: {0}", photonEvent.ToString()));
1635
1636 int actorNr = -1;
1637 PhotonPlayer originatingPlayer = null;
1638
1639 if (photonEvent.Parameters.ContainsKey(ParameterCode.ActorNr))
1640 {
1641 actorNr = (int)photonEvent[ParameterCode.ActorNr];
1642 if (this.mActors.ContainsKey(actorNr))
1643 {
1644 originatingPlayer = (PhotonPlayer)this.mActors[actorNr];
1645 }
1646 //else
1647 //{
1648 // // the actor sending this event is not in actorlist. this is usually no problem
1649 // if (photonEvent.Code != (byte)LiteOpCode.Join)
1650 // {
1651 // Debug.LogWarning("Received event, but we do not have this actor: " + actorNr);
1652 // }
1653 //}
1654 }
1655
1656 switch (photonEvent.Code)
1657 {
1658 case PunEvent.OwnershipRequest:
1659 {
1660 int[] requestValues = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1661 int requestedViewId = requestValues[0];
1662 int currentOwner = requestValues[1];
1663 Debug.Log("Ev OwnershipRequest: " + photonEvent.Parameters.ToStringFull() + " ViewID: " + requestedViewId + " from: " + currentOwner + " Time: " + Environment.TickCount%1000);
1664
1665 PhotonView requestedView = PhotonView.Find(requestedViewId);
1666 if (requestedView == null)
1667 {
1668 Debug.LogWarning("Can't find PhotonView of incoming OwnershipRequest. ViewId not found: " + requestedViewId);
1669 break;
1670 }
1671
1672 Debug.Log("Ev OwnershipRequest PhotonView.ownershipTransfer: " + requestedView.ownershipTransfer + " .ownerId: " + requestedView.ownerId + " isOwnerActive: " + requestedView.isOwnerActive + ". This client's player: " + PhotonNetwork.player.ToStringFull());
1673
1674 switch (requestedView.ownershipTransfer)
1675 {
1676 case OwnershipOption.Fixed:
1677 Debug.LogWarning("Ownership mode == fixed. Ignoring request.");
1678 break;
1679 case OwnershipOption.Takeover:
1680 if (currentOwner == requestedView.ownerId)
1681 {
1682 // a takeover is successful automatically, if taken from current owner
1683 requestedView.ownerId = actorNr;
1684 }
1685 break;
1686 case OwnershipOption.Request:
1687 if (currentOwner == PhotonNetwork.player.ID || PhotonNetwork.player.isMasterClient)
1688 {
1689 if ((requestedView.ownerId == PhotonNetwork.player.ID) || (PhotonNetwork.player.isMasterClient && !requestedView.isOwnerActive))
1690 {
1691 SendMonoMessage(PhotonNetworkingMessage.OnOwnershipRequest, new object[] {requestedView, originatingPlayer});
1692 }
1693 }
1694 break;
1695 default:
1696 break;
1697 }
1698 }
1699 break;
1700
1701 case PunEvent.OwnershipTransfer:
1702 {
1703 int[] transferViewToUserID = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1704 Debug.Log("Ev OwnershipTransfer. ViewID " + transferViewToUserID[0] + " to: " + transferViewToUserID[1] + " Time: " + Environment.TickCount%1000);
1705
1706 int requestedViewId = transferViewToUserID[0];
1707 int newOwnerId = transferViewToUserID[1];
1708
1709 PhotonView pv = PhotonView.Find(requestedViewId);
1710 pv.ownerId = newOwnerId;
1711
1712 break;
1713 }
1714 case EventCode.GameList:
1715 {
1716 this.mGameList = new Dictionary
1717 Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1718 foreach (DictionaryEntry game in games)
1719 {
1720 string gameName = (string)game.Key;
1721 this.mGameList[gameName] = new RoomInfo(gameName, (Hashtable)game.Value);
1722 }
1723 mGameListCopy = new RoomInfo[mGameList.Count];
1724 mGameList.Values.CopyTo(mGameListCopy, 0);
1725 SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1726 break;
1727 }
1728
1729 case EventCode.GameListUpdate:
1730 {
1731 Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1732 foreach (DictionaryEntry room in games)
1733 {
1734 string gameName = (string)room.Key;
1735 RoomInfo game = new RoomInfo(gameName, (Hashtable)room.Value);
1736 if (game.removedFromList)
1737 {
1738 this.mGameList.Remove(gameName);
1739 }
1740 else
1741 {
1742 this.mGameList[gameName] = game;
1743 }
1744 }
1745 this.mGameListCopy = new RoomInfo[this.mGameList.Count];
1746 this.mGameList.Values.CopyTo(this.mGameListCopy, 0);
1747 SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1748 break;
1749 }
1750
1751 case EventCode.QueueState:
1752 // not used anymore
1753 break;
1754
1755 case EventCode.AppStats:
1756 // Debug.LogInfo("Received stats!");
1757 this.mPlayersInRoomsCount = (int)photonEvent[ParameterCode.PeerCount];
1758 this.mPlayersOnMasterCount = (int)photonEvent[ParameterCode.MasterPeerCount];
1759 this.mGameCount = (int)photonEvent[ParameterCode.GameCount];
1760 break;
1761
1762 case EventCode.Join:
1763 // actorNr is fetched out of event above
1764 Hashtable actorProperties = (Hashtable)photonEvent[ParameterCode.PlayerProperties];
1765 if (originatingPlayer == null)
1766 {
1767 bool isLocal = this.mLocalActor.ID == actorNr;
1768 this.AddNewPlayer(actorNr, new PhotonPlayer(isLocal, actorNr, actorProperties));
1769 this.ResetPhotonViewsOnSerialize(); // This sets the correct OnSerializeState for Reliable OnSerialize
1770 }
1771
1772 if (actorNr == this.mLocalActor.ID)
1773 {
1774 // in this player's 'own' join event, we get a complete list of players in the room, so check if we know all players
1775 int[] actorsInRoom = (int[])photonEvent[ParameterCode.ActorList];
1776 foreach (int actorNrToCheck in actorsInRoom)
1777 {
1778 if (this.mLocalActor.ID != actorNrToCheck && !this.mActors.ContainsKey(actorNrToCheck))
1779 {
1780 this.AddNewPlayer(actorNrToCheck, new PhotonPlayer(false, actorNrToCheck, string.Empty));
1781 }
1782 }
1783
1784 // joinWithCreateOnDemand can turn an OpJoin into creating the room. Then actorNumber is 1 and callback: OnCreatedRoom()
1785 if (this.mLastJoinType == JoinType.JoinOrCreateOnDemand && this.mLocalActor.ID == 1)
1786 {
1787 SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom);
1788 }
1789 SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom); //Always send OnJoinedRoom
1790
1791 }
1792 else
1793 {
1794 SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerConnected, this.mActors[actorNr]);
1795 }
1796 break;
1797
1798 case EventCode.Leave:
1799 this.HandleEventLeave(actorNr);
1800 break;
1801
1802 case EventCode.PropertiesChanged:
1803 int targetActorNr = (int)photonEvent[ParameterCode.TargetActorNr];
1804 Hashtable gameProperties = null;
1805 Hashtable actorProps = null;
1806 if (targetActorNr == 0)
1807 {
1808 gameProperties = (Hashtable)photonEvent[ParameterCode.Properties];
1809 }
1810 else
1811 {
1812 actorProps = (Hashtable)photonEvent[ParameterCode.Properties];
1813 }
1814
1815 this.ReadoutProperties(gameProperties, actorProps, targetActorNr);
1816 break;
1817
1818 case PunEvent.RPC:
1819 //ts: each event now contains a single RPC. execute this
1820 // Debug.Log("Ev RPC from: " + originatingPlayer);
1821 this.ExecuteRPC(photonEvent[ParameterCode.Data] as Hashtable, originatingPlayer);
1822 break;
1823
1824 case PunEvent.SendSerialize:
1825 case PunEvent.SendSerializeReliable:
1826 Hashtable serializeData = (Hashtable)photonEvent[ParameterCode.Data];
1827 //Debug.Log(serializeData.ToStringFull());
1828
1829 int remoteUpdateServerTimestamp = (int)serializeData[(byte)0];
1830 short remoteLevelPrefix = -1;
1831 short initialDataIndex = 1;
1832 if (serializeData.ContainsKey((byte)1))
1833 {
1834 remoteLevelPrefix = (short)serializeData[(byte)1];
1835 initialDataIndex = 2;
1836 }
1837
1838 for (short s = initialDataIndex; s < serializeData.Count; s++)
1839 {
1840 this.OnSerializeRead(serializeData[s] as Hashtable, originatingPlayer, remoteUpdateServerTimestamp, remoteLevelPrefix);
1841 }
1842 break;
1843
1844 case PunEvent.Instantiation:
1845 this.DoInstantiate((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null);
1846 break;
1847
1848 case PunEvent.CloseConnection:
1849 // MasterClient "requests" a disconnection from us
1850 if (originatingPlayer == null || !originatingPlayer.isMasterClient)
1851 {
1852 Debug.LogError("Error: Someone else(" + originatingPlayer + ") then the masterserver requests a disconnect!");
1853 }
1854 else
1855 {
1856 PhotonNetwork.LeaveRoom();
1857 }
1858
1859 break;
1860
1861 case PunEvent.DestroyPlayer:
1862 Hashtable evData = (Hashtable)photonEvent[ParameterCode.Data];
1863 int targetPlayerId = (int)evData[(byte)0];
1864 if (targetPlayerId >= 0)
1865 {
1866 this.DestroyPlayerObjects(targetPlayerId, true);
1867 }
1868 else
1869 {
1870 if (this.DebugOut >= DebugLevel.INFO) Debug.Log("Ev DestroyAll! By PlayerId: " + actorNr);
1871 this.DestroyAll(true);
1872 }
1873 break;
1874
1875 case PunEvent.Destroy:
1876 evData = (Hashtable)photonEvent[ParameterCode.Data];
1877 int instantiationId = (int)evData[(byte)0];
1878 // Debug.Log("Ev Destroy for viewId: " + instantiationId + " sent by owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == actorNr) + " this client is owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == this.mLocalActor.ID));
1879
1880
1881 PhotonView pvToDestroy = null;
1882 if (this.photonViewList.TryGetValue(instantiationId, out pvToDestroy))
1883 {
1884 this.RemoveInstantiatedGO(pvToDestroy.gameObject, true);
1885 }
1886 else
1887 {
1888 if (this.DebugOut >= DebugLevel.ERROR) Debug.LogError("Ev Destroy Failed. Could not find PhotonView with instantiationId " + instantiationId + ". Sent by actorNr: " + actorNr);
1889 }
1890
1891 break;
1892
1893 case PunEvent.AssignMaster:
1894 evData = (Hashtable)photonEvent[ParameterCode.Data];
1895 int newMaster = (int)evData[(byte)1];
1896 this.SetMasterClient(newMaster, false);
1897 break;
1898
1899 default:
1900 if (photonEvent.Code < 200 && PhotonNetwork.OnEventCall != null)
1901 {
1902 object content = photonEvent[ParameterCode.Data];
1903 PhotonNetwork.OnEventCall(photonEvent.Code, content, actorNr);
1904 }
1905 else
1906 {
1907 // actorNr might be null. it is fetched out of event on top of method
1908 // Hashtable eventContent = (Hashtable) photonEvent[ParameterCode.Data];
1909 // this.mListener.customEventAction(actorNr, eventCode, eventContent);
1910 Debug.LogError("Error. Unhandled event: " + photonEvent);
1911 }
1912 break;
1913 }
1914
1915 this.externalListener.OnEvent(photonEvent);
1916 }
File name: PhotonNetwork.cs
Copy
855 {
856 get
857 {
858 return networkingPeer.mGameCount;
859 }
860 }
File name: GameManagerScript.cs
Copy
44 public void RestartGame () {
45 StartCoroutine ("RestartGameCoroutine");
46 }
File name: GameManagerScript.cs
Copy
48 public IEnumerator RestartGameCoroutine() {
49 player.gameObject.SetActive (false);
50 yield return new WaitForSeconds (0.5f);
51 player.transform.position = playerStartPoint;
52 groundGenerator.position = groundStartPoint;
53 player.gameObject.SetActive (true);
54 }
File name: BlockScript.cs
Copy
9 public int blockNumber; //number displayed on the block (-1 for no number)
26 public void Initialize(int x, int y, int z, GameControllerScript gameScript) {
27 this.scale = gameScript.scale;
28 this.yOffset = gameScript.yOffset;
29 this.gameScript = gameScript;
30 this.x = x;
31 this.y = y;
32 this.z = z;
33 this.originalPosition = new Vector3(x * this.scale, y * this.scale + this.yOffset, z * this.scale);
34 }
File name: ConnectorScript.cs
Copy
24 public void Initialize(int x, int y, int z, string axis, GameControllerScript gameScript) {
25 this.scale = gameScript.scale;
26 this.yOffset = gameScript.yOffset;
27 this.gameScript = gameScript;
28 this.moveDuration = gameScript.moveDuration;
29 this.x = x;
30 this.y = y;
31 this.z = z;
32 this.axis = axis;
33 this.ResetPosition();
34 }
File name: GameControllerScript.cs
Copy
248 private void adjustConnectors() {
249 int j, k, axis;
250 ConnectorScript connectorScript;
251 bool show;
252// for (j = 0; j <= 2; j++) {
253// for (k = 0; k <= 2; k++) {
254// for (axis = 0; axis <= 2; axis++) {
255// connectorScript = this.connectors[j,k,axis].gameObject.GetComponent("ConnectorScript") as ConnectorScript;
256// connectorScript.show = true;
257//
258// if (this.options.board_type == "Hollow Cube") {
259// if(j == 1 & k == 1) {
260// connectorScript.show = false;
261// }
262// }
263//
264// if (this.options.board_type == "Box Outline") { //Box Outline
265// if(j == 1 || k == 1) {
266// connectorScript.show = false;
267// }
268// }
269//
270// if (this.options.board_type == "Four Walls") {
271// //remove center connector on the x axis
272// if(axis == 0 && j == 1 & k == 1) {
273// connectorScript.show = false;
274// }
275// //remove 3 center connectors on the y axis
276// if(axis == 1 && j == 1) {
277// connectorScript.show = false;
278// }
279// //remove 3 center connectors on the z axis
280// if(axis == 2 && k == 1) {
281// connectorScript.show = false;
282// }
283// }
284//
285//
286// if (this.options.board_type == "No Corners") {
287// if(j != 1 && k != 1) {
288// connectorScript.show = false;
289// }
290// }
291// }}}
292 }
293
294 public bool doMove (string axis, int direction) {
295 int loop1, loop2; //looping variables
296 int x, y, z; //block location variables
297 int numMoves = 0; //number of moves to make
298 int scoreChange = 0;
299 bool blockCollision = false;
300 bool blockCollisionSound = false;
301 Transform blockA, blockB, blockC; //variables for holding the three blocks in a row
302 IDictionary
303 IDictionary
304 int[,,] numbersAfterMove = new int[3,3,3];
305;
306 if (PlayerPrefs.GetString ("game_status") != "playing") return false;
307 if (this.gameView != "game") return false;
308
309 //loop through each of the 9 rows to be calculated
310 for(loop1 = 0; loop1 <=2; loop1++) {
311 for(loop2 = 0; loop2 <=2; loop2++) {
312 if (axis == "x") {
313 y = loop1;
314 z = loop2;
315 if (direction == 1) {
316 blockA= this.blocks[0,y,z];
317 blockB= this.blocks[1,y,z];
318 blockC= this.blocks[2,y,z];
319 }
320 else {
321 blockA= this.blocks[2,y,z];
322 blockB= this.blocks[1,y,z];
323 blockC= this.blocks[0,y,z];
324 }
325 }
326 else if (axis == "y") {
327 x = loop1;
328 z = loop2;
329 if (direction == 1) {
330 blockA= this.blocks[x,0,z];
331 blockB= this.blocks[x,1,z];
332 blockC= this.blocks[x,2,z];
333 }
334 else {
335 blockA= this.blocks[x,2,z];
336 blockB= this.blocks[x,1,z];
337 blockC= this.blocks[x,0,z];
338 }
339 }
340 else {
341 x = loop1;
342 y = loop2;
343 if (direction == 1) {
344 blockA= this.blocks[x,y,0];
345 blockB= this.blocks[x,y,1];
346 blockC= this.blocks[x,y,2];
347 }
348 else {
349 blockA= this.blocks[x,y,2];
350 blockB= this.blocks[x,y,1];
351 blockC= this.blocks[x,y,0];
352 }
353 }
354 calculateRowChanges(this.getBlockNumber(blockA), this.getBlockNumber(blockB), this.getBlockNumber(blockC), ref scoreChange, ref newNumbers, ref shiftBy, ref blockCollision);
355
356 blockA.GetComponent
357 blockB.GetComponent
358 blockC.GetComponent
359
360 numMoves = numMoves + shiftBy["a"] + shiftBy["b"] + shiftBy["c"];
361 if (blockCollision) blockCollisionSound = true;
362
363 int newHighestBlock = 0;
364 if(newNumbers["a"] > PlayerPrefs.GetInt ("game_highest_block")) newHighestBlock = newNumbers["a"];
365 if(newNumbers["b"] > PlayerPrefs.GetInt ("game_highest_block")) newHighestBlock = newNumbers["b"];
366 if(newNumbers["c"] > PlayerPrefs.GetInt ("game_highest_block")) newHighestBlock = newNumbers["c"];
367
368 if (newHighestBlock > 0) {
369 PlayerPrefs.SetInt ("game_highest_block", newHighestBlock);
370 if (newHighestBlock == 2048) PlayerPrefs.SetString("game_status","game_won");
371 PlayerPrefs.Save ();
372 this.timer.SetNextBlockTarget(newHighestBlock);
373 if (newHighestBlock > this.getHighestBlock()) this.SetHighestBlock(newHighestBlock);
374 }
375
376
377
378
379 PlayerPrefs.Save ();
380
381 //save the numbers after the move for redo
382 numbersAfterMove[blockA.GetComponent
383 blockA.GetComponent
384 blockA.GetComponent
385
386 numbersAfterMove[blockB.GetComponent
387 blockB.GetComponent
388 blockB.GetComponent
389
390 numbersAfterMove[blockC.GetComponent
391 blockC.GetComponent
392 blockC.GetComponent
393 }
394 }
395
396 if(blockCollisionSound && this.options.play_sounds) {
397 this.collideAudioSource.PlayDelayed(this.moveDuration);
398 }
399
400 if (numMoves > 0) {
401 this.moveStartTime = Time.time;
402 this.setScore (this.score + scoreChange);
403 if(this.options.play_sounds) {
404 if (axis == "x") this.swipeAudioSource.PlayOneShot(swipeSoundX);
405 if (axis == "y") this.swipeAudioSource.PlayOneShot(swipeSoundY);
406 if (axis == "z") this.swipeAudioSource.PlayOneShot(swipeSoundZ);
407 }
408 return true;
409 }
410 else {
411 return false;
412 }
413 }
414
415 //Determine the possible successful combines between 3 blockNumbers (a,b,c) being pushed toward c
416 void calculateRowChanges (int a, int b, int c, ref int scoreChange, ref IDictionary
417 blockCollision = false;
418
419 //first check if we have any -2 values which signify that blocks cant merge along this connector
420 if(a == -2 || b == -2 || c == -2) {
421 newNumbers["a"] = a;
422 newNumbers["b"] = b;
423 newNumbers["c"] = c;
424 shiftBy["a"] = 0;
425 shiftBy["b"] = 0;
426 shiftBy["c"] = 0;
427 }
428 else {
429 //figure out if any of the three merges occurred
430 shiftBy ["c"] = 0;
431 if (c > -1 && c == b) { //b and c merged
432 scoreChange = scoreChange + c*2;
433 blockCollision = true;
434 newNumbers["c"] = c*2;
435 newNumbers["b"] = a;
436 newNumbers["a"] = -1;
437 shiftBy["b"] = 1;
438 if(a > -1) {
439 shiftBy["a"] = 1;
440 }
441 else if (a == -1) {
442 shiftBy["a"] = 0;
443 }
444 }
445 else if (b > -1 && a == b) { //a and b merged
446 scoreChange = scoreChange + a*2;
447 blockCollision = true;
448 if(c == -1) {
449 newNumbers["c"] = b*2;
450 newNumbers["b"] = -1;
451 newNumbers["a"] = -1;
452 shiftBy["b"] = 1;
453 shiftBy["a"] = 2;
454 }
455 else {
456 newNumbers["c"] = c;
457 newNumbers["b"] = b*2;
458 newNumbers["a"] = -1;
459 shiftBy["b"] = 0;
460 shiftBy["a"] = 1;
461 }
462 }
463 else if (c > -1 && a == c && b == -1) { //a and c merged
464 scoreChange = scoreChange + c*2;
465 blockCollision = true;
466 newNumbers["c"] = c*2;
467 newNumbers["b"] = -1;
468 newNumbers["a"] = -1;
469 shiftBy["b"] = 0;
470 shiftBy["a"] = 2;
471 } //end of merges block
472 else { //no merges occurred
473 if(c > -1) { //last column has number
474 newNumbers["c"] = c;
475 if(b > -1) { //second column has number
476 newNumbers["b"] = b;
477 newNumbers["a"] = a;
478 shiftBy["b"] = 0;
479 shiftBy["a"] = 0;
480 }
481 else if(b == -1) {//second column empty
482 newNumbers["b"] = a;
483 newNumbers["a"] = -1;
484 shiftBy["b"] = 0;
485 if (a == -1) {
486 shiftBy["a"] = 0;
487 }
488 else {
489 shiftBy["a"] = 1;
490 }
491 }
492
493 } //end of block for value in c column
494 else if (c == -1) { //first column empty
495 if(b > -1) { //second column has number
496 newNumbers["c"] = b;
497 newNumbers["b"] = a;
498 newNumbers["a"] = -1;
499 shiftBy["b"] = 1;
500 if(a > -1) {
501 shiftBy["a"] = 1;
502 }
503 else {
504 shiftBy["a"] = 0;
505 }
506
507 }
508 else if(b == -1) { //second column empty and first column empty
509 newNumbers["c"] = a;
510 newNumbers["b"] = -1;
511 newNumbers["a"] = -1;
512 shiftBy["b"] = 0;
513 if (a == -1) {
514 shiftBy["a"] = 0;
515 }
516 else {
517 shiftBy["a"] = 2;
518 }
519 }
520 }
521 } //end of no merges block
522 } //end of check for -2;
523 }
524
525
526 void Update (){
527 int x =0, y=0, z=0, newNumber=0;
528 if(GameControllerScript.performRestart) {
529 this.restart ();
530 GameControllerScript.performRestart = false;
531 }
532 if(moveStartTime >= 0) {
533 if(Time.time - this.moveStartTime > this.moveDuration + 0.05F) {
534 this.fillRandomBlock(ref x, ref y, ref z, ref newNumber);
535 this.saveHistory ();
536 this.moveStartTime = -1F;
537 if(this.getEmptyBlocks().Count == 0) {
538 if(this.CheckGameOver()) {
539 PlayerPrefs.SetString ("game_status", "game_over");
540 PlayerPrefs.Save();
541 }
542 }
543 }
544 }
545 else {
546 bool moved;
547 if (Input.GetKeyUp("right")) moved = this.doMove ("x", 1);
548 if (Input.GetKeyUp("left")) moved = this.doMove ("x", -1);
549 if (Input.GetKeyUp("up")) moved = this.doMove ("y", 1);
550 if (Input.GetKeyUp("down")) moved = this.doMove ("y", -1);
551 if (Input.GetKeyUp("a")) moved = this.doMove ("z", 1);
552 if (Input.GetKeyUp("z")) moved = this.doMove ("z", -1);
553 }
554 this.adjustConnectors();
555 if (Input.GetKey(KeyCode.Escape))
556 {
557 Application.Quit();
558 }
559 }
560
561 void FixedUpdate () {
562 }
563
564 void setScore(int score) {
565 this.score = score;
566 PlayerPrefs.SetInt ("score", score);
567
568 //handle setting high score
569 if(this.score > this.GetHighScore()) {
570 this.SetHighScore(this.score);
571 }
572 }
573
574 public void restart() {
575 int[,,] positions = new int[3, 3, 3];
576 int x=0, y=0, z=0, newNumber=0;
577 this.gameLight.intensity = this.lightIntensity;
578
579 for (x = 0; x <= 2; x++) {
580 for (y = 0; y <= 2; y++) {
581 for (z = 0; z <= 2; z++) {
582 this.setBlockNumber(this.blocks[x,y,z], this.getInitialBlockNumber(x,y,z));
583 positions[x,y,z] = -1;
584 }
585 }
586 }
587
588 this.adjustConnectors ();
589
590 PlayerPrefs.SetString ("redo_moves2","");
591 PlayerPrefs.SetString ("redo_moves1","");
592 PlayerPrefs.SetString ("redo_moves0","");
593 PlayerPrefs.SetInt ("redos", 2);
594 PlayerPrefs.SetString ("game_status", "playing");
595 PlayerPrefs.SetInt ("game_highest_block", 0);
596 PlayerPrefs.SetInt ("cube_rotation_x", 0);
597 PlayerPrefs.SetInt ("cube_rotation_y", 0);
598 PlayerPrefs.SetInt ("cube_rotation_z", 0);
599 PlayerPrefs.Save ();
600
601 this.fillRandomBlock(ref x, ref y, ref z, ref newNumber);
602 positions [x, y, z] = newNumber;
603 this.fillRandomBlock(ref x, ref y, ref z, ref newNumber);
604 positions [x, y, z] = newNumber;
605 this.saveHistory();
606
607 this.setScore (0);
608
609 TimerScript timerScript = this.gameObject.GetComponent ("TimerScript") as TimerScript;
610 timerScript.ResetTimer();
611 }
612
613 public int GetHighScore()
614 {
615 string key = "highscore-" + options.GetOptionsKey();
616 if (PlayerPrefs.HasKey (key)) {
617 return PlayerPrefs.GetInt (key);
618 }
619 else {
620 this.SetHighScore(0);
621 return 0;
622 }
623 }
624
625 void SetHighScore(int myHighScore)
626 {
627 string key = "highscore-" + options.GetOptionsKey();
628 PlayerPrefs.SetInt( key, myHighScore );
629 PlayerPrefs.Save();
630 }
631
632 public int getHighestBlock()
633 {
634 string key = "highestblock-" + options.GetOptionsKey();
635 if (PlayerPrefs.HasKey (key)) {
636 return PlayerPrefs.GetInt (key);
637 }
638 else {
639 this.SetHighestBlock(0);
640 return 0;
641 }
642 }
643
644 void SetHighestBlock(int highestBlock)
645 {
646 string key = "highestblock-" + options.GetOptionsKey();
647 PlayerPrefs.SetInt( key, highestBlock );
648 PlayerPrefs.Save();
649 }
650
651 private bool CheckGameOver() {
652
653 string[] axisList = new string[3] {"x", "y", "z"};
654 int direction = 1;
655 string axis = "x";
656 int loop1, loop2, loopAxis; //looping variables
657 int x, y, z; //block location variables
658 int numMoves = 0; //number of moves to make
659 int scoreChange = 0;
660 bool blockCollision = false;
661 Transform blockA, blockB, blockC; //variables for holding the three blocks in a row
662 IDictionary
663 IDictionary
664
665 //loop through each of the 9 rows to be calculated
666 for(loopAxis = 0; loopAxis < 3; loopAxis ++) {
667 axis = axisList[loopAxis];
668 for(direction = -1; direction < 2; direction += 2) {
669 for(loop1 = 0; loop1 <=2; loop1++) {
670 for(loop2 = 0; loop2 <=2; loop2++) {
671 if (axis == "x") {
672 y = loop1;
673 z = loop2;
674 if (direction == 1) {
675 blockA= this.blocks[0,y,z];
676 blockB= this.blocks[1,y,z];
677 blockC= this.blocks[2,y,z];
678 }
679 else {
680 blockA= this.blocks[2,y,z];
681 blockB= this.blocks[1,y,z];
682 blockC= this.blocks[0,y,z];
683 }
684 }
685 else if (axis == "y") {
686 x = loop1;
687 z = loop2;
688 if (direction == 1) {
689 blockA= this.blocks[x,0,z];
690 blockB= this.blocks[x,1,z];
691 blockC= this.blocks[x,2,z];
692 }
693 else {
694 blockA= this.blocks[x,2,z];
695 blockB= this.blocks[x,1,z];
696 blockC= this.blocks[x,0,z];
697 }
698 }
699 else {
700 x = loop1;
701 y = loop2;
702 if (direction == 1) {
703 blockA= this.blocks[x,y,0];
704 blockB= this.blocks[x,y,1];
705 blockC= this.blocks[x,y,2];
706 }
707 else {
708 blockA= this.blocks[x,y,2];
709 blockB= this.blocks[x,y,1];
710 blockC= this.blocks[x,y,0];
711 }
712 }
713 calculateRowChanges(this.getBlockNumber(blockA), this.getBlockNumber(blockB), this.getBlockNumber(blockC), ref scoreChange, ref newNumbers, ref shiftBy, ref blockCollision);
714
715 numMoves = numMoves + shiftBy["a"] + shiftBy["b"] + shiftBy["c"];
716 }
717 }
718 }
719 }
720 if (numMoves > 0) {
721 return false;
722 }
723 else {
724 return true;
725 }
726 }
727
728 void OnGUI() {
729 //GAME GUI
730 if (this.gameView == "game") {
731 GUI.skin = currentGUISkin;
732 this.gameLight.intensity = this.lightIntensity;
733 this.mainCamera.transform.eulerAngles = new Vector3 (16F, 29.5F, 0);
734
735 //create rotating buttons
736
737
738
739 if (PlayerPrefs.GetString ("game_status") == "game_over") {
740 GUI.Label (new Rect (0, Screen.height * 0.3f , Screen.width, Screen.height * 0.10F), "Game Over", "BigLabel");
741 this.gameLight.intensity = 0;
742 }
743 if (PlayerPrefs.GetString ("game_status") == "game_won") {
744 GUI.Label (new Rect (0, Screen.height * 0.3f , Screen.width, Screen.height * 0.10F), "You Won!", "BigLabel");
745 if (GUI.Button(new Rect(Screen.width * .33f, Screen.height * 0.4F, Screen.width * 0.30F, Screen.height * 0.06F),"Continue")) {
746 PlayerPrefs.SetString ("game_status", "playing");
747 PlayerPrefs.Save ();
748 }
749 this.gameLight.intensity = 0;
750 }
751
752 GUI.Label (new Rect (0, 0, Screen.width, Screen.height * 0.06F), "Score: " + this.score.ToString (), "BigLabel");
753 string highScoreText = "High Score/Block: " + this.GetHighScore ().ToString () + " / " + this.getHighestBlock ().ToString ();
754 GUI.Label (new Rect (0, Screen.height * 0.06F, Screen.width, Screen.height / 10), highScoreText, "SmallLabel");
755
756
757 GUIStyle style = currentGUISkin.GetStyle ("button");
758 //style.fontSize = 14;
759
760 if (GUI.Button(new Rect(1, Screen.height * 0.12F, Screen.width * 0.30F, Screen.height * 0.06F),"Menu")) {
761 this.gameView = "menu";
762 }
763 if (GUI.Button(new Rect(Screen.width * .33f, Screen.height * 0.12F, Screen.width * 0.33F, Screen.height * 0.06F),"Restart")) {
764 this.restart();
765 }
766 if (GUI.Button(new Rect(Screen.width * .7f, Screen.height * 0.12F, Screen.width * .3f, Screen.height * 0.06F), "Undo (" + PlayerPrefs.GetInt ("redos").ToString () + ")")) {
767 this.undo();
768 }
769
770
771 //Roation buttons
772
773
774 //GUI.Label(new Rect(Screen.width * .8f, Screen.height * 0.72F, Screen.width * .1f, Screen.height * 0.06F), "", "RotateButton");
775
776 //left button
777 if (GUI.Button(new Rect(Screen.width * .73f, Screen.height * 0.72F, Screen.width * .1f, Screen.height * 0.06F), "\t", "RotateButtonLeft")) {
778 this.rotateBlocks(Vector3.up);
779 }
780 //right button
781 if (GUI.Button(new Rect(Screen.width * .87f, Screen.height * 0.72F, Screen.width * .1f, Screen.height * 0.06F), "", "RotateButtonRight")) {
782 this.rotateBlocks(Vector3.down);
783 }
784 //up button
785 if (GUI.Button(new Rect(Screen.width * .8f, Screen.height * 0.675F, Screen.width * .1f, Screen.height * 0.06F), "", "RotateButtonUp")) {
786 this.rotateBlocks(Vector3.right);
787 }
788 //down button
789 if (GUI.Button(new Rect(Screen.width * .8f, Screen.height * 0.765F, Screen.width * .1f, Screen.height * 0.06F), "", "RotateButtonDown")) {
790 this.rotateBlocks(Vector3.left);
791 }
792 }
793 }
794
795 private void sizeGUI() {
796 this.currentGUISkin.GetStyle ("Label").fontSize = Mathf.CeilToInt(Screen.height * 0.04F);
797 this.currentGUISkin.GetStyle ("Button").fontSize = Mathf.CeilToInt(Screen.height * 0.04F);
798 this.currentGUISkin.GetStyle ("Subheader").fontSize = Mathf.CeilToInt(Screen.height * 0.05F);
799 this.currentGUISkin.GetStyle ("Toggle").fontSize = Mathf.CeilToInt(Screen.height * 0.04F);
800 this.currentGUISkin.GetStyle ("ToggleLabel").fontSize = Mathf.CeilToInt(Screen.height * 0.04F);
801 this.currentGUISkin.GetStyle ("ToggleLabelWarning").fontSize = Mathf.CeilToInt(Screen.height * 0.04F);
802
803 this.currentGUISkin.GetStyle ("SmallLabel").fontSize = Mathf.CeilToInt(Screen.height * 0.03F);
804
805 this.currentGUISkin.GetStyle ("BigLabel").fontSize = Mathf.CeilToInt(Screen.height * 0.06F);
806
807 this.currentGUISkin.GetStyle ("Toggle").padding.top = Mathf.CeilToInt(Screen.height * 0.04F);
808 this.currentGUISkin.GetStyle ("Toggle").padding.left = Mathf.CeilToInt(Screen.height * 0.04F);
809 }
810
811 private void saveHistory() {
812 int x,y,z;
813 string stringVal = "";
814
815 //create comma separated list of values
816 for (x = 0; x <= 2; x++) {
817 for (y = 0; y <= 2; y++) {
818 for (z = 0; z <= 2; z++) {
819 stringVal = stringVal + getBlockNumber(this.blocks[x,y,z]).ToString() + ",";
820 }
821 }
822 }
823 //add score and highest block to end of array
824 stringVal = stringVal + this.score.ToString() + "," + PlayerPrefs.GetInt ("game_highest_block").ToString();
825
826 PlayerPrefs.SetString ("redo_moves2",PlayerPrefs.GetString ("redo_moves1"));
827 PlayerPrefs.SetString ("redo_moves1",PlayerPrefs.GetString ("redo_moves0"));
828 PlayerPrefs.SetString ("redo_moves0",stringVal);
829
830 PlayerPrefs.Save();
831 }
832
833 private void undo() {
834 string[] positions = new string[27];
835 int myNum = 0;
836 char[] separator = {','};
837
838 if( PlayerPrefs.GetString ("game_status") != "game_over"
839 && PlayerPrefs.GetString ("redo_moves1") != ""
840 && PlayerPrefs.GetInt ("redos") > 0
841 ) {
842 positions = PlayerPrefs.GetString ("redo_moves1").Split (separator);
843 //loop through and set the block numbers for each block
844 for (int x = 0; x <= 2; x++) {
845 for (int y = 0; y <= 2; y++) {
846 for (int z = 0; z <= 2; z++) {
847 int.TryParse(positions[9*x+3*y+z], out myNum);
848 this.setBlockNumber(this.blocks[x,y,z],myNum);
849 }
850 }
851 }
852
853 //roll back the score
854 int.TryParse (positions[27], out myNum);
855 this.setScore (myNum);
856
857 //roll back the game_highest_block
858 int.TryParse (positions[28], out myNum);
859 PlayerPrefs.SetInt ("game_highest_block", myNum);
860
861 //move all the redo moves back
862 PlayerPrefs.SetString ("redo_moves0",PlayerPrefs.GetString ("redo_moves1"));
863 PlayerPrefs.SetString ("redo_moves1",PlayerPrefs.GetString ("redo_moves2"));
864 PlayerPrefs.SetString ("redo_moves2","");
865 PlayerPrefs.SetInt ("redos", PlayerPrefs.GetInt ("redos") - 1);
866 }
867 }
868
869 private void rotateBlocks(Vector3 direction) {
870 Transform block;
871 BlockScript blockScript;
872 Transform connector;
873 ConnectorScript connectorScript;
874 this.rotating = true;
875 for (int x = 0; x <= 2; x++) {
876 for (int y = 0; y <= 2; y++) {
877 for (int z = 0; z <= 2; z++) {
878 block = this.blocks[x,y,z];
879 block.GetComponent
880
881 for(int axis = 0; axis <=2; axis++) {
882 connector = this.connectors[x,y,z,axis];
883 connector.GetComponent
884 }
885 }
886 }
887 }
888 }
889
890 private void loadSavedGame() {
891 string[] positions = new string[27];
892 int positionNum = 0;
893 char[] separator = {','};
894 positions = PlayerPrefs.GetString ("redo_moves0").Split (separator);
895
896 for (int x = 0; x <= 2; x++) {
897 for (int y = 0; y <= 2; y++) {
898 for (int z = 0; z <= 2; z++) {
899 int.TryParse(positions[9*x+3*y+z], out positionNum);
900 this.setBlockNumber(this.blocks[x,y,z],positionNum);
901 }
902 }
903 }
904
905
906 this.adjustConnectors ();
907 this.setScore (PlayerPrefs.GetInt ("score"));
908
909 }
910}
911
912
File name: InstructionsScript.cs
Copy
16 void Start () {
17 this.gameScript = this.gameObject.GetComponent ("GameControllerScript") as GameControllerScript;
18 this.currentGUISkin = gameScript.currentGUISkin;
19
20 }
File name: MenuScript.cs
Copy
10 void Start () {
11 this.gameScript = this.gameObject.GetComponent ("GameControllerScript") as GameControllerScript;
12 this.currentGUISkin = gameScript.currentGUISkin;
13
14 }
Download file with original file name:GameC
GameC 93 lượt xem
Gõ tìm kiếm nhanh...