1
+ var setUpComponents = function ( ) {
2
+
3
+ Crafty . c ( 'Bar' , {
4
+ init : function ( ) {
5
+ this . requires ( '2D, DOM, Color, Tween' )
6
+ } ,
7
+
8
+ resolve : function ( object , volume , music ) {
9
+ this . requires ( 'Mouse' )
10
+ this . bind ( 'Click' , function ( e ) {
11
+ if ( music ) {
12
+ if ( this . _w + this . _x - 5 < e . realX ) {
13
+ volume . music_vol = 1.0
14
+ object . tween ( { w : 200 } , 20 ) ;
15
+ } else if ( this . _x + 5 > e . realX ) {
16
+ volume . music_vol = 1.0
17
+ object . tween ( { w : 0 } , 20 ) ;
18
+ } else {
19
+ volume . music_vol = ( e . realX - ( this . _x + 10 ) ) / 200
20
+ object . tween ( { w : e . realX - ( this . _x + 5 ) } , 20 ) ;
21
+ }
22
+ Crafty . audio . play ( "background" , - 1 , change , true )
23
+ }
24
+ else {
25
+ if ( this . _w + this . _x - 5 < e . realX ) {
26
+ volume . sound_vol = 1.0
27
+ object . tween ( { w : 200 } , 20 ) ;
28
+ } else if ( this . _x + 5 > e . realX ) {
29
+ volume . sound_vol = 1.0
30
+ object . tween ( { w : 0 } , 20 ) ;
31
+ } else {
32
+ volume . sound_vol = ( e . realX - ( this . _x + 10 ) ) / 200
33
+ object . tween ( { w : e . realX - ( this . _x + 5 ) } , 20 ) ;
34
+ }
35
+ }
36
+ } )
37
+ }
38
+ } )
39
+
40
+ Crafty . c ( 'Grid' , {
41
+ init : function ( ) {
42
+ this . attr ( { w : tileWidth , h : tileHeight } )
43
+ } ,
44
+
45
+ // Locate this entity at the given position on the grid
46
+ at : function ( x , y ) {
47
+ if ( x === undefined && y === undefined ) {
48
+ return { x : this . x / tileWidth , y : this . y / tileHeight }
49
+ } else {
50
+ this . attr ( { x : x * tileWidth , y : y * tileHeight } ) ;
51
+ return this ;
52
+ }
53
+ }
54
+ } ) ;
55
+
56
+ Crafty . c ( 'Actor' , {
57
+ init : function ( ) {
58
+ this . requires ( '2D, DOM, Grid' ) ;
59
+ }
60
+ } ) ;
61
+
62
+ Crafty . c ( 'Exit' , {
63
+ init : function ( ) {
64
+ this . requires ( 'Actor' ) ;
65
+ } ,
66
+ setID : function ( ID ) {
67
+ this . ID = ID ;
68
+ }
69
+ } ) ;
70
+
71
+ Crafty . c ( 'Door' , {
72
+ init : function ( ) {
73
+ this . requires ( 'Exit, doorway, SpriteAnimation' ) . animate ( "phase" , 0 , 0 , 2 ) ;
74
+ this . animate ( "phase" , 80 , - 1 )
75
+ } ,
76
+ setID : function ( ID ) {
77
+ this . ID = ID ;
78
+ return this ;
79
+ }
80
+ } ) ;
81
+
82
+ Crafty . c ( 'Flower' , {
83
+ init : function ( ) {
84
+ this . requires ( 'Actor, flower, SpriteAnimation' ) . animate ( "wind" , 0 , 1 , 3 ) ;
85
+ this . animate ( "wind" , 80 , - 1 )
86
+ }
87
+ } ) ;
88
+
89
+ Crafty . c ( 'PlayerCharacter' , {
90
+ init : function ( ) {
91
+ this . requires ( 'Actor, Fourway, player, SpriteAnimation, Collision, Persist' )
92
+ . fourway ( 3 )
93
+ . stopOnSolids ( )
94
+ . exitOnDoors ( )
95
+ . attr ( { z : 3 } )
96
+ . animate ( 'PlayerMovingUp' , 3 , 3 , 5 )
97
+ . animate ( 'PlayerMovingRight' , 9 , 3 , 11 )
98
+ . animate ( 'PlayerMovingDown' , 0 , 3 , 2 )
99
+ . animate ( 'PlayerMovingLeft' , 6 , 3 , 8 ) ;
100
+
101
+ var animation_speed = 15 ;
102
+ this . bind ( 'NewDirection' , function ( data ) {
103
+ this . stop ( ) ;
104
+ if ( data . x > 0 ) {
105
+ this . animate ( 'PlayerMovingRight' , animation_speed , - 1 ) ;
106
+ } else if ( data . x < 0 ) {
107
+ this . animate ( 'PlayerMovingLeft' , animation_speed , - 1 ) ;
108
+ } else if ( data . y > 0 ) {
109
+ this . animate ( 'PlayerMovingDown' , animation_speed , - 1 ) ;
110
+ } else if ( data . y < 0 ) {
111
+ this . animate ( 'PlayerMovingUp' , animation_speed , - 1 ) ;
112
+ }
113
+ } ) ;
114
+ } ,
115
+
116
+ stopOnSolids : function ( ) {
117
+ this . onHit ( 'Solid' , this . stopMovement ) ;
118
+ return this ;
119
+ } ,
120
+
121
+ stopMovement : function ( ent ) {
122
+ if ( this . _movement ) {
123
+ for ( var i = 0 ; i < ent . length ; i ++ ) {
124
+ var obj = ent [ i ] . obj ;
125
+ var up = ( ! obj . contains ( this . _x , this . _y , 2 , 0 ) ) && ( ! obj . contains ( this . _x + this . _w - 2 , this . _y , 2 , 0 ) ) ;
126
+ var down = ( ! obj . contains ( this . _x , this . _y + this . _h , 2 , 0 ) ) && ( ! obj . contains ( this . _x + this . _w - 2 , this . _y + this . _h , 2 , 0 ) ) ;
127
+ var left = ( ! obj . contains ( this . _x , this . _y , 0 , 2 ) ) && ( ! obj . contains ( this . _x , this . _y + this . _h - 2 , 0 , 2 ) ) ;
128
+ var right = ( ! obj . contains ( this . _x + this . _w , this . _y , 0 , 2 ) ) && ( ! obj . contains ( this . _x + this . _w , this . _y + this . _h - 2 , 0 , 2 ) ) ;
129
+ if ( ( this . _movement . y > 0 && ! down ) || ( this . _movement . y < 0 && ! up ) ) {
130
+ this . y -= ( this . _movement . y * 1.1 ) ;
131
+ }
132
+ if ( ( this . _movement . x > 0 && ! right ) || ( this . _movement . x < 0 && ! left ) ) {
133
+ this . x -= ( this . _movement . x * 1.1 ) ;
134
+ }
135
+ }
136
+ }
137
+ } ,
138
+
139
+ exitOnDoors : function ( ) {
140
+ this . onHit ( 'Door' , function ( ent ) {
141
+ var door = ent [ 0 ] . obj ;
142
+ if ( door . contains ( this . _x + ( this . _w / 2 ) - 5 , this . _y + ( this . _h / 2 ) - 5 , 10 , 10 ) ) {
143
+ Crafty . audio . play ( "doorSound" ) ;
144
+ exitLoc = door . at ( ) ;
145
+ load_scene ( door . ID , 40 ) ;
146
+ }
147
+ } ) ;
148
+ return this ;
149
+ }
150
+ } ) ;
151
+ }
0 commit comments