What's new? | Help | Directory | Sign in
Google
heron-language
Heron is an object-oriented programming language for model driven architecture
  
  
  
  
    
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
TODO:
- seperate this into multiple domains
- add interfaces
- add support for tags
- add some basic type-inference
- increase usage of immutability
- mutable or immutable tags?
- add verification phase
- add bridge operations and domains
- support "auto" transitions
- provide built-in support for "KeyEvents"
*/


domain Demo {

imports {
Math;
Drawing2D;
Keyboard;
}

attributes {
painter : Painter;
shooter : Shooter;
walls : Collection<Wall>;
balls : Collection<Ball>;
}

operations {
initialize() {
var x0 : int = 100;
var y0 : int = 100;
var x1 : int = 300;
var y1 : int = 300;
walls = new Collection<Wall>();
balls = new Collection<Ball>();
shooter = new Shooter(0, 10, 10);
walls.add(new Wall(x0,y0,x1,y0));
walls.add(new Wall(x1,y0,x1,y1));
walls.add(new Wall(x1,y1,x0,y1));
walls.add(new Wall(x0,y1,x0,y0));
balls.add(new Ball(new Point(200,200), new Vector(200,120), 10));
balls.add(new Ball(new Point(150,150), new Vector(100,60), 15));
balls.add(new Ball(new Point(250,150), new Vector(-50,-5), 20));
balls.add(new Ball(new Point(250,250), new Vector(-100,0), 25));
balls.add(new Ball(new Point(150,250), new Vector(0,100), 30));
painter = new Painter();
painter.generateNextEvent();
}

distance(first : Point, second : Point) : Real {
return sqrt(sqr(first.x - second.x) + sqr(first.y - second.y));
}
sqrt(x : Real) : Real {
return Math.sqrt(x);
}
sqr(x : Real) : Real {
return x * x;
}
acos(x : Real) : Real {
return Math.acos(x);
}
min(x : Real, y : Real) : Real {
return Math.min(x, y);
}
floor(x : Real) : int {
return (int)Math.floor(x);
}
updateBallPositions(elapsedMSec : Real) {
foreach (ball : Ball in Demo.balls) {
ball.updatePosition(elapsedMSec);
}
}
}

classes {
class Wall {
attributes {
line : Line;
}
operations {
constructor(x0 : Real, y0 : Real, x1 : Real, y1 : Real) {
line = new Line(x0, y0, x1, y1);
}
}
}

class Shooter {
attributes {
angle : Real;
length : Real;
width : Real;
}
operations {
construct(angle : Real, length : Real, width : Real) {
this.angle = angle;
this.length = length;
this.width = width;
}
getPoints() : Collection<Point> {
var result = new Collection<Point>();
var pt1 : Point = new Point(-(width / 2), length / 2));
var pt2 : Point = new Point(width / 2, length / 2);
var pt3 : Point = new Point(0, -(length / 2));
result.Add(pt1);
result.Add(pt2);
result.Add(pt3);
return result;
}
}

class Ball {
attributes {
pos : Point;
vec : Vector;
radius : Real;
}
operations {
constructor(pos : Point, vec : Vector, radius : Real) {
this.pos = pos;
this.vec = vec;
this.radius = radius;
}
computeWallCollisionEvent(wall : Wall) : BallWallCollisionEvent {
var x0 : Real = pos.x;
var y0 : Real = pos.y;
var xt : Real = vec.x;
var yt : Real = vec.y;
var x1 : Real = wall.line.begin.x;
var y1 : Real = wall.line.begin.y;
var x2 : Real = wall.line.end.x;
var y2 : Real = wall.line.end.y;
var den : Real = Demo.sqrt(Demo.sqr(x2 - x1) + Demo.sqr(y2 - y1));
var dx : Real = x2 - x1;
var dy : Real = y2 - y1;

var t1 : Real = ((radius * den) - (dx * y1) + (dx * y0) + (dy * x1) - (dy * x0)) / (-dx * yt + dy * xt);
var t2 : Real = (-(radius * den) - (dx * y1) + (dx * y0) + (dy * x1) - (dy * x0)) / (-dx * yt + dy * xt);
var t : Real = Demo.min(t1, t2);
if (t1 <= 0.0) {
if (t2 <= 0.0) {
return null;
}
else {
t = t2;
}
}
else {
if (t2 < 0) {
t = t1;
}
else {
t = Demo .min (t1 , t2 ) ;
}
}
return new CollisionEvent(Demo.floor(t * 1000), this, wall);
}
computeCollisionEvents() : Collection<CollisionEvent> {
var result : Collection<CollisionEvent> = new Collection<CollisionEvent>();
foreach (wall : Wall in Demo.walls) {
CollisionEvent collision = computeCollisionEvent(wall);
if (collision != null) {
result.add(collision);
}
}
return result;
}
bounceOffWall(wall : Wall) {
var w : Vector = new Vector(wall.line);
var n : Vector = w.normal();
var pn : Vector = vec.proj(n);
var pw : Vector = vec.proj(w);
var r : Vector = pn.scale(-1.0).add(pw);
vec.x = r.x;
vec.y = r.y;
}
// Alternatively, this could be implemented as a state.
updatePosition(elapsedMSec : Real) {
pos = pos.translate(vec.scale(elapsedMSec / 1000.0));
}
}
}

class BallBallCollisionEvent {
attributes {
timeElapsed : Real;
ball1 : Ball;
ball2 : Ball;
}
operations {
constructor(timeElapsed : Real, ball1 : Ball, ball2 : Ball) {
this.timeElapsed = timeElapsed;
this.ball1 = ball1;
this.ball2 = ball2;
}
}
}

class BallWallCollisionEvent {
attributes {
timeElapsed : Real;
ball : Ball;
wall : Wall;
}
operations {
constructor(timeElapsed : Real, ball : Ball, wall : Wall) {
this.timeElapsed = timeElapsed;
this.ball = ball;
this.wall = wall;
}
}
}

class PaintEvent {
attributes {
timeElapsed : Real;
}
operations {
constructor(timeElapsed : Real) {
this.timeElapsed = timeElapsed;
}
}
}

class EventGenerator {
}

class Painter {
attributes {
paintFrequency : Real;
timeToNextPaint : Real;
}
operations {
constructor() {
paintFrequency = 50.0;
timeToNextPaint = paintFrequency;
}
generateNextEvent() {
generateNextEvent(null, null);
}
generateNextEvent(lastBall : Ball, lastWall : Wall) {
var next : CollisionEvent;
var q : Collection<CollisionEvent> = new Collection<CollisionEvent>();

// Compute all possible CollisionEvents
q.clear();
foreach (ball : Ball in Demo.balls) {
q.concat(ball.computeCollisionEvents());
}

// Exclude last ball/wall CollisionEvent
q.filter((x : CollisionEvent)
=> { return (x.ball != lastBall) || (x.wall != lastWall); });

// Get the next earliest CollisionEvent
next = q.min((x : CollisionEvent) => { return x.timeElapsed; });

if (next != null) {
if (next.timeElapsed < timeToNextPaint) {
timeToNextPaint = timeToNextPaint - next.timeElapsed;
this.sendIn(next, (int)next.timeElapsed);
}
else {
this.sendIn(new PaintEvent(timeToNextPaint), (int)timeToNextPaint);
}
}
else {
Demo.error("could not compute next collision");
}
}
drawShooter() {
// Note: a "fold" function would be more elegant
var pts : Collection<Point> = Demo.shooter.getPoints();
var i : int = 1;
while (i < pts.Count) {
var pt1 : Point = pts.getAt(i - 1);
var pt2 : Point = pts.getAt(i);
Demo.drawLine(pt1.x, pt1.y, pt2.x, pt2.y);
++i;
}
if (pts.Count > 0) {
var pt1 : Point = pts.getAt(0);
Demo.drawLine(
}
}
}
states {
initial() {
transitions {
BallWallCollisionEvent -> onBallWallCollision;
BallBallCollisionEvent -> onBallBallCollsion;
PaintEvent -> onPaint;
KeyboardEvent -> onKeyboard;
}
}
onCollision(col : CollisionEvent) {
entry {
Demo.updateBallPositions(col.timeElapsed);
col.ball.bounceOffWall(col.wall);
generateNextEvent(col.ball, col.wall);
}
transitions {
auto -> initial;
}
}
onPaint(evt : PaintEvent) {
entry {
timeToNextPaint = paintFrequency;
Demo.clear();
Demo.updateBallPositions(evt.timeElapsed);

foreach (wall : Wall in Demo.walls) {
Demo.drawLine(wall.line.begin.x, wall.line.begin.y, wall.line.end.x, wall.line.end.y);
}

foreach (ball : Ball in Demo.balls) {
Demo.drawCircle(ball.pos.x, ball.pos.y, ball.radius);
}

drawShooter();
Demo.render();
generateNextEvent();
}
transitions {
auto -> initial;
}
}
onKey(evt : KeyEvent) {
entry {
if (evt.Key == Keys.LeftArrow) {
Demo.shooter.TurnLeft();
}
else if (evt.Key == Keys.RightArrow) {
Demo.shooter.TurnRight();
}
else if (evt.Key == Keys.Space) {
Demo.shooter.Shoot();
}
}
transitions {
auto -> initial;
}
}
}
}
}
}
Show details Hide details

Change log

r43 by cdiggins on Mar 25, 2008   Diff
Improved C++ compliance of YARD library.
Started using unit tests again. Made a
bunch of improvements.
Go to: 
Project members, sign in to write a code review

Older revisions

r31 by cdiggins on Feb 19, 2008   Diff
Now works as an applet.
r29 by cdiggins on Feb 19, 2008   Diff
OMG it works!!
r28 by cdiggins on Feb 18, 2008   Diff
Drawing and movement but no collision
yet. Still working on that.
All revisions of this file

File info

Size: 10219 bytes, 349 lines