PageRenderTime 157ms CodeModel.GetById 75ms app.highlight 3ms RepoModel.GetById 77ms 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
 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
12 Instead of creating a new trivial subclass, considering creating a trait and attaching it to that object
13
14 For example to make an entity invincible for a period of time you might make a trait like this
15
16 [PSUEDO CODE START]
17 // Inside a trait subclass
18 attach: function(anEntity)
19 {
20     this.callSuper();
21     this.intercept(['onHit', 'getShotPower']);
22 },
23
24 onHit: function() {
25 		// Do nothing, im invincible!
26 	},
27
28 getShotStrength: function() {
29 		return 100000000; // OMGBBQ! Thats high!
30 	}
31 [PSUEDO CODE END]
32
33 Be sure to call restore before detaching the trait!
34
35 Basic Usage:
36
37 // Let my character be controlled by the KB
38 if(newEntity.connectionID === this.netChannel.connectionID) {
39		aCharacter.addTraitAndExecute( new ClientControlledTrait() );
40		this.clientCharacter = aCharacter;
41	}
42 */
43(function () {
44    RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.controller.traits");
45
46    RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait = function () {
47        RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.superclass.constructor.call(this);
48    };
49
50    RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.prototype = {
51        displayName: "KeyboardInputTrait",					// Unique string name for this Trait
52        /**
53         * Attach the trait to the host object
54         * @param anEntity
55         */
56        attach: function (anEntity) {
57            RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait.superclass.attach.call(this, anEntity);
58
59            // Intercept those two properties from the attached enitity with our own
60            this.intercept(['constructEntityDescription', 'handleInput']);
61            this.attachedEntity.input = new RealtimeMultiplayerGame.Input.Keyboard();
62            this.attachedEntity.input.attachEvents();
63        },
64
65        /**
66         * Implement our own intercepted version of the methods/properties
67         */
68        constructEntityDescription: function (gameTick, wantsFullUpdate) {
69            return {
70                entityid: this.entityid,
71                input: this.input.constructInputBitmask()
72            }
73        },
74
75        // Do nothing
76        handleInput: function (gameClock) {
77        }
78    };
79
80    RealtimeMultiplayerGame.extend(RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
81})();