| OLD | NEW |
| 1 part of game; | 1 part of game; |
| 2 | 2 |
| 3 const double _steeringThreshold = 20.0; | 3 const double _steeringThreshold = 0.0; |
| 4 const double _steeringMax = 50.0; | 4 const double _steeringMax = 150.0; |
| 5 | 5 |
| 6 class GameBox extends SpriteBox { | 6 class GameDemoBox extends SpriteBox { |
| 7 | 7 |
| 8 GameBox(GameWorld game) : super(game, SpriteBoxTransformMode.letterbox); | 8 GameDemoBox(GameDemoWorld game) : super(game, SpriteBoxTransformMode.letterbox
); |
| 9 | 9 |
| 10 GameWorld get _gameWorld => this.rootNode; | 10 GameDemoWorld get _gameWorld => this.rootNode; |
| 11 | 11 |
| 12 // Handle pointers | 12 // Handle pointers |
| 13 int _firstPointer = -1; | 13 int _firstPointer = -1; |
| 14 int _secondPointer = -1; | 14 int _secondPointer = -1; |
| 15 Vector2 _firstPointerDownPos; | 15 Point _firstPointerDownPos; |
| 16 | 16 |
| 17 void handleEvent(Event event, BoxHitTestEntry entry) { | 17 void handleEvent(Event event, BoxHitTestEntry entry) { |
| 18 if (event is PointerEvent) { | 18 if (event is PointerEvent) { |
| 19 Vector2 pointerPos = new Vector2(event.x, event.y); | 19 Point pointerPos = new Point(event.x, event.y); |
| 20 int pointer = event.pointer; | 20 int pointer = event.pointer; |
| 21 | 21 |
| 22 switch (event.type) { | 22 switch (event.type) { |
| 23 case 'pointerdown': | 23 case 'pointerdown': |
| 24 if (_firstPointer == -1) { | 24 if (_firstPointer == -1) { |
| 25 // Assign the first pointer | 25 // Assign the first pointer |
| 26 _firstPointer = pointer; | 26 _firstPointer = pointer; |
| 27 _firstPointerDownPos = pointerPos; | 27 _firstPointerDownPos = pointerPos; |
| 28 } | 28 } |
| 29 else if (_secondPointer == -1) { | 29 else if (_secondPointer == -1) { |
| 30 // Assign second pointer | 30 // Assign second pointer |
| 31 _secondPointer = pointer; | 31 _secondPointer = pointer; |
| 32 _gameWorld.controlThrust(1.0); | 32 _gameWorld.controlFire(); |
| 33 } | 33 } |
| 34 else { | 34 else { |
| 35 // There is a pointer used for steering, let's fire instead | 35 // There is a pointer used for steering, let's fire instead |
| 36 _gameWorld.controlFire(); | 36 _gameWorld.controlFire(); |
| 37 } | 37 } |
| 38 break; | 38 break; |
| 39 case 'pointermove': | 39 case 'pointermove': |
| 40 if (pointer == _firstPointer) { | 40 if (pointer == _firstPointer) { |
| 41 // Handle turning control | 41 // Handle turning control |
| 42 double deltaX = pointerPos[0] - _firstPointerDownPos[0]; | 42 double joystickX = 0.0; |
| 43 double deltaX = pointerPos.x - _firstPointerDownPos.x; |
| 43 if (deltaX > _steeringThreshold || deltaX < -_steeringThreshold) { | 44 if (deltaX > _steeringThreshold || deltaX < -_steeringThreshold) { |
| 44 double turnForce = (deltaX - _steeringThreshold)/(_steeringMax - _
steeringThreshold); | 45 joystickX = (deltaX - _steeringThreshold)/(_steeringMax - _steerin
gThreshold); |
| 45 if (turnForce > 1.0) turnForce = 1.0; | 46 if (joystickX > 1.0) joystickX = 1.0; |
| 46 if (turnForce < -1.0) turnForce = -1.0; | 47 if (joystickX < -1.0) joystickX = -1.0; |
| 47 _gameWorld.controlSteering(turnForce); | |
| 48 print("steering: $turnForce"); | |
| 49 } | 48 } |
| 49 |
| 50 double joystickY = 0.0; |
| 51 double deltaY = pointerPos.y - _firstPointerDownPos.y; |
| 52 if (deltaY > _steeringThreshold || deltaY < -_steeringThreshold) { |
| 53 joystickY = (deltaY - _steeringThreshold)/(_steeringMax - _steerin
gThreshold); |
| 54 if (joystickY > 1.0) joystickY = 1.0; |
| 55 if (joystickY < -1.0) joystickY = -1.0; |
| 56 } |
| 57 |
| 58 _gameWorld.controlSteering(joystickX, joystickY); |
| 50 } | 59 } |
| 51 break; | 60 break; |
| 52 case 'pointerup': | 61 case 'pointerup': |
| 53 case 'pointercancel': | 62 case 'pointercancel': |
| 54 if (pointer == _firstPointer) { | 63 if (pointer == _firstPointer) { |
| 55 // Un-assign the first pointer | 64 // Un-assign the first pointer |
| 56 _firstPointer = -1; | 65 _firstPointer = -1; |
| 57 _firstPointerDownPos = null; | 66 _firstPointerDownPos = null; |
| 58 _gameWorld.controlSteering(null); | 67 _gameWorld.controlSteering(0.0, 0.0); |
| 59 } | 68 } |
| 60 else if (pointer == _secondPointer) { | 69 else if (pointer == _secondPointer) { |
| 61 _secondPointer = -1; | 70 _secondPointer = -1; |
| 62 _gameWorld.controlThrust(null); | 71 _gameWorld.controlFire(); |
| 63 } | 72 } |
| 64 break; | 73 break; |
| 65 default: | 74 default: |
| 66 break; | 75 break; |
| 67 } | 76 } |
| 68 } | 77 } |
| 69 } | 78 } |
| 70 | 79 |
| 71 } | 80 } |
| OLD | NEW |