PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/js/controller/traits/KeyboardInputTrait.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 81 lines | 24 code | 6 blank | 51 comment | 0 complexity | a291c0752080a629e35de0c3e462ad58 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. File:
  3. BaseTrait.js
  4. Created By:
  5. Mario Gonzalez
  6. Project :
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. Traits work by effectively 'hi-jacking' properties of their attachedEntity.
  10. These properties can by functions, or non-primitive data types.
  11. Instead of creating a new trivial subclass, considering creating a trait and attaching it to that object
  12. For example to make an entity invincible for a period of time you might make a trait like this
  13. [PSUEDO CODE START]
  14. // Inside a trait subclass
  15. attach: function(anEntity)
  16. {
  17. this.callSuper();
  18. this.intercept(['onHit', 'getShotPower']);
  19. },
  20. onHit: function() {
  21. // Do nothing, im invincible!
  22. },
  23. getShotStrength: function() {
  24. return 100000000; // OMGBBQ! Thats high!
  25. }
  26. [PSUEDO CODE END]
  27. Be sure to call restore before detaching the trait!
  28. Basic Usage:
  29. // Let my character be controlled by the KB
  30. if(newEntity.connectionID === this.netChannel.connectionID) {
  31. aCharacter.addTraitAndExecute( new ClientControlledTrait() );
  32. this.clientCharacter = aCharacter;
  33. }
  34. */
  35. (function () {
  36. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.controller.traits");
  37. RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait = function () {
  38. RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.superclass.constructor.call(this);
  39. };
  40. RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.prototype = {
  41. displayName: "KeyboardInputTrait", // Unique string name for this Trait
  42. /**
  43. * Attach the trait to the host object
  44. * @param anEntity
  45. */
  46. attach: function (anEntity) {
  47. RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.superclass.attach.call(this, anEntity);
  48. // Intercept those two properties from the attached enitity with our own
  49. this.intercept(['constructEntityDescription', 'handleInput']);
  50. this.attachedEntity.input = new RealtimeMultiplayerGame.Input.Keyboard();
  51. this.attachedEntity.input.attachEvents();
  52. },
  53. /**
  54. * Implement our own intercepted version of the methods/properties
  55. */
  56. constructEntityDescription: function (gameTick, wantsFullUpdate) {
  57. return {
  58. entityid: this.entityid,
  59. input: this.input.constructInputBitmask()
  60. }
  61. },
  62. // Do nothing
  63. handleInput: function (gameClock) {
  64. }
  65. };
  66. RealtimeMultiplayerGame.extend(RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
  67. })();