当前位置:  首页>> 技术小册>> cocos游戏开发入门

PlayerController:

  1. import { _decorator, Component, Vec3, EventMouse, input, Input, Animation } from "cc";
  2. const { ccclass, property } = _decorator;
  3. export const BLOCK_SIZE = 40;
  4. @ccclass("PlayerController")
  5. export class PlayerController extends Component {
  6. @property(Animation)
  7. BodyAnim:Animation = null;
  8. private _startJump: boolean = false;
  9. private _jumpStep: number = 0;
  10. private _curJumpTime: number = 0;
  11. private _jumpTime: number = 0.1;
  12. private _curJumpSpeed: number = 0;
  13. private _curPos: Vec3 = new Vec3();
  14. private _deltaPos: Vec3 = new Vec3(0, 0, 0);
  15. private _targetPos: Vec3 = new Vec3();
  16. private _curMoveIndex: number = 0;
  17. start () {
  18. //input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
  19. }
  20. setInputActive(active: boolean) {
  21. if (active) {
  22. input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
  23. } else {
  24. input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
  25. }
  26. }
  27. reset() {
  28. this._curMoveIndex = 0;
  29. }
  30. onMouseUp(event: EventMouse) {
  31. if (event.getButton() === 0) {
  32. this.jumpByStep(1);
  33. } else if (event.getButton() === 2) {
  34. this.jumpByStep(2);
  35. }
  36. }
  37. jumpByStep(step: number) {
  38. if (this._startJump) {
  39. return;
  40. }
  41. this._startJump = true;
  42. this._jumpStep = step;
  43. this._curJumpTime = 0;
  44. const clipName = step == 1 ? 'oneStep' : 'twoStep';
  45. const state = this.BodyAnim.getState(clipName);
  46. this._jumpTime = state.duration;
  47. this._curJumpSpeed = this._jumpStep * BLOCK_SIZE/ this._jumpTime;
  48. this.node.getPosition(this._curPos);
  49. Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep* BLOCK_SIZE, 0, 0));
  50. if (this.BodyAnim) {
  51. if (step === 1) {
  52. this.BodyAnim.play('oneStep');
  53. } else if (step === 2) {
  54. this.BodyAnim.play('twoStep');
  55. }
  56. }
  57. this._curMoveIndex += step;
  58. }
  59. onOnceJumpEnd() {
  60. this.node.emit('JumpEnd', this._curMoveIndex);
  61. }
  62. update (deltaTime: number) {
  63. if (this._startJump) {
  64. this._curJumpTime += deltaTime;
  65. if (this._curJumpTime > this._jumpTime) {
  66. // end
  67. this.node.setPosition(this._targetPos);
  68. this._startJump = false;
  69. this.onOnceJumpEnd();
  70. } else {
  71. // tween
  72. this.node.getPosition(this._curPos);
  73. this._deltaPos.x = this._curJumpSpeed * deltaTime;
  74. Vec3.add(this._curPos, this._curPos, this._deltaPos);
  75. this.node.setPosition(this._curPos);
  76. }
  77. }
  78. }
  79. }

GameManager.ts:

  1. import { _decorator, CCInteger, Component, instantiate, Label, math, Node, Prefab, Vec3 } from 'cc';
  2. import { BLOCK_SIZE, PlayerController } from './PlayerController';
  3. const { ccclass, property } = _decorator;
  4. enum BlockType {
  5. BT_NONE,
  6. BT_STONE,
  7. };
  8. enum GameState {
  9. GS_INIT,
  10. GS_PLAYING,
  11. GS_END,
  12. };
  13. @ccclass('GameManager')
  14. export class GameManager extends Component {
  15. @property({ type: Prefab })
  16. public boxPrefab: Prefab | null = null;
  17. @property({ type: CCInteger })
  18. public roadLength: number = 50;
  19. private _road: BlockType[] = [];
  20. @property({ type: Node })
  21. public startMenu: Node | null = null;
  22. @property({ type: PlayerController })
  23. public playerCtrl: PlayerController | null = null;
  24. @property({ type: Label })
  25. public stepsLabel: Label | null = null;
  26. start() {
  27. this.setCurState(GameState.GS_INIT);
  28. this.playerCtrl?.node.on('JumpEnd', this.onPlayerJumpEnd, this);
  29. }
  30. init() {
  31. if (this.startMenu) {
  32. this.startMenu.active = true;
  33. }
  34. this.generateRoad();
  35. if (this.playerCtrl) {
  36. this.playerCtrl.setInputActive(false);
  37. this.playerCtrl.node.setPosition(Vec3.ZERO);
  38. this.playerCtrl.reset();
  39. }
  40. }
  41. setCurState(value: GameState) {
  42. switch (value) {
  43. case GameState.GS_INIT:
  44. this.init();
  45. break;
  46. case GameState.GS_PLAYING:
  47. if (this.startMenu) {
  48. this.startMenu.active = false;
  49. }
  50. if (this.stepsLabel) {
  51. this.stepsLabel.string = '0'; // 将步数重置为0
  52. }
  53. setTimeout(() => { //直接设置active会直接开始监听鼠标事件,做了一下延迟处理
  54. if (this.playerCtrl) {
  55. this.playerCtrl.setInputActive(true);
  56. }
  57. }, 0.1);
  58. break;
  59. case GameState.GS_END:
  60. break;
  61. }
  62. }
  63. generateRoad() {
  64. this.node.removeAllChildren();
  65. this._road = [];
  66. // startPos
  67. this._road.push(BlockType.BT_STONE);
  68. for (let i = 1; i < this.roadLength; i++) {
  69. if (this._road[i - 1] === BlockType.BT_NONE) {
  70. this._road.push(BlockType.BT_STONE);
  71. } else {
  72. this._road.push(Math.floor(Math.random() * 2));
  73. }
  74. }
  75. for (let j = 0; j < this._road.length; j++) {
  76. let block: Node | null = this.spawnBlockByType(this._road[j]);
  77. if (block) {
  78. this.node.addChild(block);
  79. block.setPosition(j * BLOCK_SIZE, 0, 0);
  80. }
  81. }
  82. }
  83. spawnBlockByType(type: BlockType) {
  84. if (!this.boxPrefab) {
  85. return null;
  86. }
  87. let block: Node | null = null;
  88. switch (type) {
  89. case BlockType.BT_STONE:
  90. block = instantiate(this.boxPrefab);
  91. break;
  92. }
  93. return block;
  94. }
  95. onStartButtonClicked() {
  96. this.setCurState(GameState.GS_PLAYING);
  97. }
  98. checkResult(moveIndex: number) {
  99. if (moveIndex < this.roadLength) {
  100. if (this._road[moveIndex] == BlockType.BT_NONE) { //跳到了空方块上
  101. this.setCurState(GameState.GS_INIT);
  102. }
  103. } else { // 跳过了最大长度
  104. this.setCurState(GameState.GS_INIT);
  105. }
  106. }
  107. onPlayerJumpEnd(moveIndex: number) {
  108. if (this.stepsLabel) {
  109. this.stepsLabel.string = '' + (moveIndex >= this.roadLength ? this.roadLength : moveIndex);
  110. }
  111. this.checkResult(moveIndex);
  112. }
  113. }

该分类下的相关小册推荐:

暂无相关推荐.