【Unity XLua】热更新XLua入门(二):俄罗斯方块实例篇
发表于2017-05-21
前言
在XLua没出来之前,开源的lua框架基本都是以界面用Lua开发为主,核心战斗用C#开发,但xLua出来之后主推C#开发,Lua用作HotFix,这里我展示的第一个例子就是基于界面的经典2D小游戏——俄罗斯方块,界面逻辑是用C#写,启动加载逻辑是用lua,后面我会继续第二个同样的Demo,但是以纯Lua为主,这个案例明天更新。
效果图
游戏启动
C#启动Lua逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | using UnityEngine; using System.Collections; using System.Collections.Generic; using XLua; using System; [LuaCallCSharp] public class LuaRunningBehaviour : MonoBehaviour { public TextAsset luaScript; internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only! internal static float lastGCTime = 0; internal const float GCInterval = 1; //1 second private Action luaStart; private Action luaUpdate; private Action luaOnDestroy; private LuaTable scriptEnv; void Awake() { scriptEnv = luaEnv.NewTable(); LuaTable meta = luaEnv.NewTable(); meta.Set( "__index" , luaEnv.Global); scriptEnv.SetMetaTable(meta); meta.Dispose(); scriptEnv.Set( "self" , this ); luaEnv.DoString(luaScript.text, "LuaBehaviour" , scriptEnv); Action luaAwake = scriptEnv.Get( "awake" ); scriptEnv.Get( "start" , out luaStart); scriptEnv.Get( "update" , out luaUpdate); scriptEnv.Get( "ondestroy" , out luaOnDestroy); if (luaAwake != null ) { luaAwake(); } } // Use this for initialization void Start() { if (luaStart != null ) { luaStart(); } } // Update is called once per frame void Update() { if (luaUpdate != null ) { luaUpdate(); } if (Time.time - LuaBehaviour.lastGCTime > GCInterval) { luaEnv.Tick(); LuaBehaviour.lastGCTime = Time.time; } } void OnDestroy() { if (luaOnDestroy != null ) { luaOnDestroy(); } luaOnDestroy = null ; luaUpdate = null ; luaStart = null ; scriptEnv.Dispose(); } } |
Lua加载游戏界面预设逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | local util = require 'xlua.util' local function initGamePanel() local obj = CS.UnityEngine.Resources.Load( "TetrisPanelOne" ) local gamePanel = CS.UnityEngine.Object.Instantiate(obj.gameObject).transform gamePanel.name = "GamePanel" gamePanel.gameObject.name = "GamePanel" gamePanel:SetParent(self.transform) gamePanel.localPosition = CS.UnityEngine.Vector3.zero gamePanel.localScale = CS.UnityEngine.Vector3.one end function start() print( "lua start, 游戏开始..." ) initGamePanel() end function ondestroy() print( "lua destroy" ) end |
游戏主要界面对应的C#逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 | using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class MainGameCSScript : MonoBehaviour { const int cRow = 16; const int cCol = 10; List< int >[] aTable = new List< int >[cRow]; int lineCount = 0; public Texture[] BoxTexture = new Texture[4]; private string [] boxColors = new string [] { "eluosi_lan" , "eluosi_zi" , "eluosi_lv" , "eluosi_hong" }; public int LineCount { get { return lineCount; } set { lineCount = value; this .onLineCountChange(); } } int score = 0; public int Score { get { return score; } set { score = value; this .onScoreChange(); } } int nextCellID = 0; int curCellID = 0; float curSpeed = 1; float fCurSpeed = 1; float fDeltaTime = 0; bool bPause = false ; bool bStart = false ; bool bGameOver = false ; public bool BGameOver { get { return bGameOver; } set { bGameOver = value; this .onGameOverChange(bGameOver); } } System.Random rdm = new System.Random(); const int boxTop = 16; const int boxLeft = 290; ///
/// 彩色砖块的宽度 /// const int boxSize = 38; int [][] nextCell = null ; int [][] curCell = null ; int curCellCol = 0; int curCellRow = 0; public GameObject BtnUp, BtnDown, BtnLeft, BtnRight, BtnIsPause; public UILabel TimeLabel, ScoreLabel, LineCountLabel; private Action onScoreChange = null ; private Action onLineCountChange = null ; private Action< bool > onGameOverChange = null ; public GameObject EndPanel, BtnRestart; public UISprite[] BoxSprites = null ; void Start() { BoxSprites = new UISprite[4]; for ( int i = 0; i < 4; i++) { BoxTexture[i] = Resources.Load(boxColors[i]); } this .onScoreChange = this .ScoreChange; this .onLineCountChange = this .LineCountChange; this .onGameOverChange = this .GameOverChange; TimeLabel = transform.Find( "LeftNode/CutDownBG/CountLabel" ).GetComponent(); LineCountLabel = transform.Find( "ScoreNode/LineCount" ).GetComponent(); ScoreLabel = transform.Find( "ScoreNode/Score" ).GetComponent(); BtnLeft = transform.Find( "InputNode/Left" ).gameObject; BtnRight = transform.Find( "InputNode/Right" ).gameObject; BtnUp = transform.Find( "InputNode/Up" ).gameObject; BtnDown = transform.Find( "InputNode/Down" ).gameObject; BtnIsPause = transform.Find( "LeftNode/RestartBtn" ).gameObject; EndPanel = transform.Find( "EndPanel" ).gameObject; BtnRestart = transform.Find( "EndPanel/ReplayBtn" ).gameObject; for ( int i = 1; i <= 4; i++) { var obj = transform.Find( string .Format( "BlcokTop/Blocks/block{0}" , i)).gameObject; var sprite = obj.GetComponent(); BoxSprites[i - 1] = sprite; } UIEventListener.Get(BtnRestart).onClick = go => { EndPanel.gameObject.SetActive( false ); Debug.Log( "重新开始" ); DoStart(); }; UIEventListener.Get(BtnIsPause).onClick = go => { Debug.Log( "开始/暂停" ); bPause = !bPause; }; UIEventListener.Get(BtnUp).onClick = go => { Debug.Log( "上翻" ); DoTransform(); }; UIEventListener.Get(BtnDown).onPress = (go, state) => { Debug.Log( "下按" ); //state true:按下 false:松开 if (state) DoSpeedUp(); else DoSpeedRestore(); }; UIEventListener.Get(BtnLeft).onClick = go => { Debug.Log( "左移" ); DoLeft(); }; UIEventListener.Get(BtnRight).onClick = go => { Debug.Log( "右移" ); DoRight(); }; this .DoStart(); } void ScoreChange() { ScoreLabel.text = "获得积分:" + this .Score.ToString(); } void LineCountChange() { LineCountLabel.text = "消灭行数:" + this .LineCount.ToString(); } void GameOverChange( bool state) { if (state) EndPanel.SetActive( true ); } ///
/// 初始化10*16的格子 /// void DoInit() { this .LineCount = 0; this .Score = 0; curSpeed = 1; for ( int i = 0; i < aTable.Length; i++) { aTable[i] = new List< int >(); for ( int j = 0; j < cCol; j++) aTable[i].Add(0); } } void OnGUI() { if (!bStart) return ; if (BGameOver) { return ; } if (bPause) { return ; } for ( int i = 0; i < curCell.Length; i++) { for ( int j = 0; j < curCell[i].Length; j++) { if (curCell[i][j] == 0) { continue ; } if (i + curCellRow < 0) { continue ; } GUI.DrawTexture( new Rect(boxLeft + boxSize * j + curCellCol * boxSize, boxTop + boxSize * i + curCellRow * boxSize, boxSize, boxSize), BoxTexture[0]); } } //背景图 for ( int i = 0; i < aTable.Length; i++) { for ( int j = 0; j < cCol; j++) { if (aTable[i][j] == 0) { continue ; } GUI.DrawTexture( new Rect(boxLeft + boxSize * j, boxTop + boxSize * i, boxSize, boxSize), BoxTexture[aTable[i][j] - 1]); } } } string GetContent( int cellContent) { switch (cellContent) { case 0: return "" ; default : return cellContent.ToString(); } } void DoStart() { DoInit(); DoNextCell(); bStart = true ; BGameOver = false ; } void DrawNextBox() { //绘制下一个图 int boxIndex = 0; for ( int i = 0; i < nextCell.Length; i++) { for ( int j = 0; j < nextCell[i].Length; j++) { if (nextCell[i][j] == 0) { continue ; } BoxSprites[boxIndex++].transform.localPosition = new Vector3(j * 35, i * (-35), 0); } } } ///
/// 随机生成下面一个模型 /// void DoNextCell() { if (nextCell == null ) { nextCellID = rdm.Next(aCells.Length); nextCell = aCells[nextCellID]; } curCellCol = cCol / 2 - 2; curCellRow = -4; curCell = nextCell; curCellID = nextCellID; nextCellID = rdm.Next(aCells.Length); nextCell = aCells[nextCellID]; DrawNextBox(); DetectIsFail(); } ///
/// 放置当前的模型 /// private void DoSetCurCellDown() { for ( int i = 0; i < curCell.Length; i++) { for ( int j = 0; j < curCell[i].Length; j++) { if (curCell[i][j] == 0) { continue ; } if (curCellRow + i < 0) { BGameOver = true ; return ; } aTable[curCellRow + i][curCellCol + j] = curCell[i][j]; } } //速度重置 DoSpeedRestore(); //消除一行 DoLine(); //生成下一个模型 DoNextCell(); } void Update() { if (!bStart) { return ; } if (BGameOver) { return ; } if (bPause) { return ; } //自动下滑 fDeltaTime += Time.deltaTime; if (fDeltaTime > fCurSpeed) { fDeltaTime -= fCurSpeed; if (CanMoveTo(curCellCol, curCellRow + 1)) { curCellRow++; } else { DoSetCurCellDown(); return ; } } if (BGameOver) { return ; } if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) //左移 { DoLeft(); } if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) //右移 { DoRight(); } if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) //上翻 { DoTransform(); } if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) //按下快速下移 { DoSpeedUp(); } if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow)) //按键取消 取消快速下移 { DoSpeedRestore(); } } ///
/// 检测游戏是否结束 /// private void DetectIsFail() { if (CanMoveTo(curCellCol, curCellRow)) { return ; } BGameOver = true ; } ///
/// 判断模型移动 /// /// /// /// /// bool CanMoveTo( int x, int y, int [][] cell = null ) // 3, -4 { if (cell == null ) { cell = curCell; } for ( int i = 0; i < cell.Length; i++) { for ( int j = 0; j < cell[i].Length; j++) { if (cell[i][j] == 0) { continue ; } if (x + j < 0 || x + j >= cCol) { return false ; } if (y + i >= cRow) //到顶了 { return false ; } if (y + i < 0) { continue ; } if (aTable[i + y][j + x] != 0) { return false ; } } } return true ; } private void DoSpeedRestore() { fCurSpeed = curSpeed; } //下移加速 private void DoSpeedUp() { fCurSpeed = curSpeed / 8; fDeltaTime = fCurSpeed; } //右移 private void DoRight() { if (CanMoveTo(curCellCol + 1, curCellRow)) { curCellCol++; } } //左移 private void DoLeft() { if (CanMoveTo(curCellCol - 1, curCellRow)) { curCellCol--; } } //上翻 private void DoTransform() { int transformedCellID = curCellID / 4 * 4 + (curCellID % 4 + 1) % 4; if (CanMoveTo(curCellCol, curCellRow, aCells[transformedCellID])) { curCellID = transformedCellID; curCell = aCells[transformedCellID]; } } ///
/// 消除一行 /// private void DoLine() { List< int > fulledLines = new List< int >(); for ( int i = curCellRow; i < curCellRow + 4; i++) { if (i >= cRow) { continue ; } if (i < 0) { BGameOver = true ; return ; } for ( int j = 0; j < cCol; j++) { if (aTable[i][j] == 0) { break ; } if (j == cCol - 1) { fulledLines.Add(i); } } } if (fulledLines.Count == 0) { return ; } for ( int i = 0; i < fulledLines.Count; i++) { for ( int j = 0; j < cCol; j++) { aTable[fulledLines[i]][j] = 0; } } int ilastEmptyRow = fulledLines[fulledLines.Count - 1]; int ilastNotEmptyRow = ilastEmptyRow - 1; while ( true ) { if (ilastNotEmptyRow < 0) { break ; } if (!IsRowEmpty(ilastNotEmptyRow)) { for ( int j = 0; j < cCol; j++) { aTable[ilastEmptyRow][j] = aTable[ilastNotEmptyRow][j]; aTable[ilastNotEmptyRow][j] = 0; } ilastEmptyRow--; } ilastNotEmptyRow--; } for ( int i = 0; i < fulledLines.Count; i++) { for ( int j = 0; j < cCol; j++) { aTable[i][j] = 0; } } //消除的行数 LineCount += fulledLines.Count; //分数加成 Score += fulledLines.Count * fulledLines.Count; curSpeed *= 0.99f; } bool IsRowEmpty( int irow) { for ( int j = 0; j < cCol; j++) { if (aTable[irow][j] != 0) { return false ; } } return true ; } ///
/// 各种形态的砖块 28个 /// private int [][][] aCells = new int [][][] { new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,1}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{0,1,1,0}, new int []{0,0,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,1}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{0,1,1,0}, new int []{0,0,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,0,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,0}, new int []{0,1,1,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,0,0}, new int []{0,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,0}, new int []{0,1,1,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,1,1}, new int []{0,0,0,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,1,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,1,1}, new int []{0,0,0,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,1,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{1,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{0,1,1,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,0,0}, new int []{1,1,1,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{1,1,0,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,0}, new int []{1,1,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,0,0}, new int []{0,1,0,0}, new int []{0,1,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,1,0}, new int []{1,0,0,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,1,1,0}, new int []{0,1,0,0}, new int []{0,1,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,1,1,0}, new int []{0,0,1,0}, new int []{0,0,0,0}, }, new int [][] { new int []{0,0,0,0}, new int []{0,0,1,0}, new int []{0,0,1,0}, new int []{0,1,1,0}, }, new int [][] { new int []{0,0,0,0}, new int []{1,0,0,0}, new int []{1,1,1,0}, new int []{0,0,0,0}, }, }; } int > int > int > bool > int > int > |