1
1
var game = {
2
2
blockSize : 30 ,
3
+ ground : null ,
4
+ solids : null ,
5
+ physics : null ,
3
6
objects : [ ]
4
7
} ;
5
8
6
9
function makeClasses ( ) {
10
+ game . solids = new Group ( ) ;
11
+ game . physics = new Group ( ) ;
12
+ game . ground = new Group ( ) ;
13
+
7
14
game . Block = function ( x , y , w , h ) {
8
15
this . sprite = createSprite ( x , y , w , h ) ;
9
16
} ;
10
17
game . Block . prototype = {
11
- tick : function ( ) { } ,
12
- collideWith : function ( target ) { }
18
+ tick : function ( ) { } ,
19
+ interact : function ( ) { } ,
20
+ collideWith : function ( target ) { }
13
21
} ;
14
22
15
23
game . Ground = function ( x , y , w , h ) {
16
24
game . Block . call ( this , x , y , w , h ) ;
17
25
this . sprite . shapeColor = color ( 80 , 180 , 100 ) ;
18
- //this.sprite.immovable = true;
26
+ this . sprite . immovable = true ;
27
+ game . solids . add ( this . sprite ) ;
28
+ game . ground . add ( this . sprite ) ;
19
29
} ;
20
30
game . Ground . prototype = {
21
31
tick : function ( ) {
22
32
game . Block . prototype . tick . call ( this ) ;
23
33
} ,
34
+ interact : function ( ) {
35
+ game . Block . prototype . interact . call ( this ) ;
36
+ } ,
24
37
collideWith : function ( target ) {
25
- console . log ( target ) ;
26
38
game . Block . prototype . collideWith . call ( this , target ) ;
27
- target . sprite . velocity . y = 0 ;
28
39
}
29
40
} ;
30
41
31
42
game . PhysicsObject = function ( x , y , w , h ) {
32
43
game . Block . call ( this , x , y , w , h ) ;
33
- this . gravity = createVector ( 0 , 0.2 ) ;
44
+ game . solids . add ( this . sprite ) ;
45
+ game . physics . add ( this . sprite ) ;
46
+ this . gravity = createVector ( 0 , 1 ) ;
34
47
} ;
35
48
game . PhysicsObject . prototype = {
36
49
tick : function ( ) {
37
50
game . Block . prototype . tick . call ( this ) ;
38
51
this . sprite . velocity . add ( this . gravity ) ;
39
52
} ,
53
+ interact : function ( ) {
54
+ game . Block . prototype . interact . call ( this ) ;
55
+ } ,
40
56
collideWith : function ( target ) {
41
57
game . Block . prototype . collideWith . call ( this , target ) ;
58
+ if ( this . sprite . touching . bottom ) {
59
+ this . sprite . velocity . y = this . gravity . y ;
60
+ }
42
61
}
43
62
} ;
44
63
@@ -50,6 +69,10 @@ function makeClasses() {
50
69
game . Player . prototype = {
51
70
tick : function ( ) {
52
71
game . PhysicsObject . prototype . tick . call ( this ) ;
72
+ } ,
73
+ interact : function ( ) {
74
+ game . PhysicsObject . prototype . interact . call ( this ) ;
75
+ console . log ( this . sprite . velocity . y ) ;
53
76
if ( keyDown ( 'RIGHT_ARROW' ) ) {
54
77
this . sprite . velocity . x = 5 ;
55
78
} else if ( keyDown ( 'LEFT_ARROW' ) ) {
@@ -58,8 +81,12 @@ function makeClasses() {
58
81
this . sprite . velocity . x = 0 ;
59
82
}
60
83
61
- if ( keyWentDown ( 'UP_ARROW' ) && this . sprite . velocity . y == 0 ) {
62
- this . sprite . velocity . y = this . jump . y ;
84
+ if ( keyWentDown ( 'UP_ARROW' ) ) {
85
+ this . sprite . position . y += 2 ;
86
+ if ( this . sprite . touching . bottom ) {
87
+ this . sprite . velocity . y = this . jump . y ;
88
+ }
89
+ this . sprite . position . y -= 2 ;
63
90
}
64
91
} ,
65
92
collideWith : function ( target ) {
@@ -69,24 +96,27 @@ function makeClasses() {
69
96
}
70
97
71
98
function setup ( ) {
99
+ createCanvas ( 800 , 500 ) ;
72
100
makeClasses ( ) ;
73
- createCanvas ( 800 , 600 ) ;
74
- objects = [ new game . Player ( 400 , 300 ) , new game . Ground ( 200 , 400 , 300 , 30 ) ] ;
101
+ game . objects = [ new game . Player ( 400 , 300 ) , new game . Ground ( 400 , 400 , 300 , 80 ) ] ;
75
102
}
76
103
104
+ var n = 0 ;
77
105
function draw ( ) {
78
106
background ( 255 , 220 , 180 ) ;
79
- objects . forEach ( function ( obj ) {
107
+ game . objects . forEach ( function ( obj ) {
80
108
obj . tick ( ) ;
81
109
} ) ;
82
- for ( var i = 0 ; i < objects . length ; i ++ ) {
83
- for ( var j = i + 1 ; j < objects . length ; j ++ ) {
84
- if ( objects [ i ] . sprite . overlap ( objects [ j ] . sprite ) ) {
85
- console . log ( "touching" ) ;
86
- objects [ i ] . collideWith ( objects [ j ] ) ;
87
- objects [ j ] . collideWith ( objects [ i ] ) ;
110
+ for ( var i = 0 ; i < game . objects . length ; i ++ ) {
111
+ for ( var j = i + 1 ; j < game . objects . length ; j ++ ) {
112
+ if ( game . objects [ i ] . sprite . collide ( game . objects [ j ] . sprite ) ) {
113
+ game . objects [ i ] . collideWith ( game . objects [ j ] ) ;
114
+ game . objects [ j ] . collideWith ( game . objects [ i ] ) ;
88
115
} ;
89
116
}
90
117
}
118
+ game . objects . forEach ( function ( obj ) {
119
+ obj . interact ( ) ;
120
+ } ) ;
91
121
drawSprites ( ) ;
92
122
}
0 commit comments