-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple_bucket_level.coffee
161 lines (140 loc) · 4.61 KB
/
simple_bucket_level.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
view.level = 'simple_bucket'
Peanutty.createEnvironment()
# Zoom out
peanutty.setScale(25 * (peanutty.canvas.width() / 835))
# Create the cannon area
peanutty.createGround
x: 60
y: 20
width: 100
height: 10
peanutty.createBall
x: 80
y: 40
radius: 15
static: true
cannon = peanutty.createBox
x: 70
y: 80
width: 60
height: 20
static: true
cannon.SetPositionAndAngle(cannon.GetPosition(), (Math.PI * 3/4))
# Create the ball area
peanutty.createGround
x: 400
y: 400
width: 100
height: 10
ball = peanutty.createBall
x: 400
y: 440
radius: 20
drawData:
color: new b2d.Common.b2Color(0, 0, 0.8)
alpha: 0.8
# Create the bucket
bucket = peanutty.createPoly
fixtureDefs: [
peanutty.polyFixtureDef
path: [{x: 600, y: 280},{x: 610, y: 280},{x: 610, y: 180},{x: 600, y: 180}]
peanutty.polyFixtureDef
path: [{x: 610, y: 190},{x: 700, y: 190},{x: 700, y: 180},{x: 610, y: 180}]
userData: {bottom: true}
peanutty.polyFixtureDef
path: [{x: 700, y: 280},{x: 710, y: 280},{x: 710, y: 180},{x: 700, y: 180}]
]
static: true
bucketBottom = peanutty.searchObjectList(
object: bucket.GetFixtureList()
searchFunction: (fixture) -> fixture.GetUserData()? && fixture.GetUserData().bottom
limit: 1
)[0]
# Listen for the ball in the bucket
peanutty.addContactListener
listener: (contact) =>
return if view.levelElements.success?
fixtures = [contact.GetFixtureA(), contact.GetFixtureB()]
if ball.GetFixtureList() in fixtures and bucketBottom in fixtures
success = view.levelElements.success = $(document.createElement("DIV"))
success.addClass('level_element')
success.css
textAlign: 'center'
position: 'absolute'
top: '180px'
left: "#{peanutty.canvas.width() * 6/11}"
success.html(
"""
<h4>Success! Nice Job!</h4>
<p>
Got a creative solution?
Let me know:
<a href='http://twitter.com/jaredcosulich' target='_blank'>@jaredcosulich</a>
</p>
<p>On to the <a href='#level/a_little_code'>next level ></a>...</p>
<p>
... or <a href='#create'>create your own level!<a>
</p>
"""
)
view.$('#canvas_container').append(success)
# Set up the user inputs
title = view.levelElements.title = $(document.createElement("DIV"))
title.css
width: '500px'
textAlign: 'center'
fontSize: '20pt'
position: 'absolute'
top: '20px'
left: "#{(peanutty.canvas.width() / 2) - 250}px"
title.html("Get the Blue Ball in to the Bucket")
view.$('#canvas_container').append(title)
cannonControl = view.levelElements.cannonControl = $(document.createElement("DIV"))
cannonControl.css
fontSize: '12pt'
position: 'absolute'
top: '60px'
left: "20px"
cannonControl.html(
"""
<h5>Cannon Controls</h5>
<p>Angle: <input id='cannon_angle' type='text' style='width: 2em' value=45 />°</p>
<p>Force: <input id='cannon_force' type='text' style='width: 2em' value=10 /></p>
<a id='fire_cannon' class="btn error">
Fire Cannon!
</a>
<a id='try_again' class="btn primary" style='display: none;'>
Try Again
</a>
"""
)
view.$('#canvas_container').append(cannonControl)
# Cannon Firing
view.$('#fire_cannon').bind 'click', () =>
peanutty.addToScript
command:
"""
cannonball = peanutty.createBall
x: 125
y: 133
radius: 10
density: 50
drawData: {color: new b2d.Common.b2Color(0.1, 0.1, 0.1), alpha: 0.8}
angle = #{view.$('#cannon_angle').val()}
force = #{view.$('#cannon_force').val()}
x = Math.cos(Math.PI/(180 / angle)) * force
y = -1 * Math.sin(Math.PI/(180 / angle)) * force
cannonball.SetLinearVelocity(new b2d.Common.Math.b2Vec2(x,y))
"""
time: 0
view.$('#fire_cannon').hide()
view.$('#try_again').show()
view.$('#try_again').bind 'click', () =>
angleVal = view.$('#cannon_angle').val()
forceVal = view.$('#cannon_force').val()
level.reset()
view.$('#cannon_angle').val(angleVal)
view.$('#cannon_force').val(forceVal)
view.$('#try_again').hide()
view.$('#fire_cannon').show()
peanutty.sign('@jaredcosulich', 'jaredcosulich')