-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nim
156 lines (128 loc) · 3.86 KB
/
main.nim
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
import
globals,
physics/chipmunk,
csfml,
math,
random
randomize()
## Filters
const
cBorder = 0b0001.BitMask
cBall = 0b0010.BitMask
cBox = 0b0100.BitMask
let
FilterBorder = chipmunk.ShapeFilter(
group: nil,
categories: cBorder,
mask: cBorder or cBall or cBox
)
FilterBall = chipmunk.ShapeFilter(
group:nil,
categories: cBall,
mask: cBorder or cBall
)
FilterBox = chipmunk.ShapeFilter(
group:nil,
categories: cBox,
mask: cBorder or cBox
)
## Collision types
let
ctBorder = cast[CollisionType](1)
ctBall = cast[CollisionType](2)
ctBox = cast[CollisionType](3)
## Types
type
FixtureType = enum
ftRect
ftCircle
Fixture = ref object
fixtureType: FixtureType
body: chipmunk.Body
shape: chipmunk.Shape
sprite: csfml.RectangleShape
World = ref object
world: Space
spawn: Vect
objects: seq[Fixture]
proc setColor(this: RectangleShape, color: Color) =
this.fillColor = color
proc newWorld(): World =
result = World()
result.world = newSpace()
result.world.gravity = Vect(x: 0, y:300.0)
result.spawn = Vect(x: 100.0, y:100.0)
result.objects = newSeq[Fixture]()
proc newRectObject(this: World, x: float, y: float, width: float, height: float, mass: float, isStatic: bool): Fixture =
result = Fixture()
result.fixtureType = ftRect
## Create the sprite for this game object
result.sprite = newRectangleShape()
result.sprite.size = Vector2f(x: width, y: height)
result.sprite.origin = Vector2f(x: width/2, y: height/2)
result.sprite.fillColor = White
## Create a dynamic or static body
if isStatic:
result.body = this.world.addBody(newStaticBody())
else:
result.body = this.world.addBody(newBody(mass, momentForBox(mass, width, height)))
result.body.position = Vect(x: x, y: y)
## Create the shape for this body
result.shape = this.world.addShape(newBoxShape(result.body, width, height, radius = 0.0))
result.shape.filter = FilterBox
result.shape.collisionType = ctBox
## Init Window
var window = newRenderWindow(
videoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Game", WindowStyle.Default
)
## Set framerate
window.framerateLimit = 60
## Create the world
var world = newWorld()
## Create the floor
var floor = world.newRectObject(WINDOW_WIDTH.float / 2f, WINDOW_HEIGHT.float - 30f, WINDOW_WIDTH.float, 30f, 50f, true)
floor.sprite.setColor(Color(r: 160, g: 160, b: 160, a: 255))
world.objects.add(floor)
## Create some random falling rects
var count: int = 0
let colors = [Red, Blue, Green, Yellow, Magenta, Cyan]
while count < 5:
var rw = rand(50..90)
var rh = rand(50..100)
var rect = world.newRectObject(100.0 * count.float, 300f, rw.float, rh.float, 50f, false)
rect.sprite.setColor(sample(colors))
world.objects.add(rect)
count.inc
## Create falling rect
var testRectA = world.newRectObject(WINDOW_WIDTH.float / 2.0, 100f, 100f, 100f, 50f, false)
## Add the rect to the world
world.objects.add(testRectA)
## Main loop
var event: Event
while window.open():
while window.pollEvent(event):
if event.kind == EventType.Closed:
window.close()
break
elif event.kind == EventType.KeyPressed:
if event.key.code == KeyCode.Escape:
window.close()
break
elif event.kind == MouseButtonPressed:
if $event.mouseButton.button == "Left":
testRectA.body.velocity = Vect(x: 0, y: 0)
testRectA.body.position = Vect(x: event.mouseButton.x.float, y: event.mouseButton.y.float)
break
world.world.step(1.0/60.0)
window.clear(Black)
for o in world.objects:
if o.fixtureType == ftRect:
o.sprite.position = Vector2f(x: o.body.position.x.float, y: o.body.position.y.float)
o.sprite.rotation = o.body.angle.radToDeg()
window.draw(o.sprite)
window.display()
## Cleanup on quit
for o in world.objects:
o.body.destroy()
o.sprite.destroy()
world.world.destroy()