Permalink
Browse files

Actors, collisions.

  • Loading branch information...
1 parent c7daf4f commit 40162571515c3c1899632bd0b01e811130086ee4 Amos Wenger committed Aug 22, 2012
Showing with 147 additions and 3 deletions.
  1. +25 −0 source/ldkit/Actor.ooc
  2. +97 −0 source/ldkit/Collision.ooc
  3. +12 −1 source/ldkit/Engine.ooc
  4. +5 −0 source/ldkit/Math.ooc
  5. +8 −2 source/ldkit/UI.ooc
View
@@ -0,0 +1,25 @@
+
+Actor: class {
+
+ init: func {}
+
+ update: func (delta: Float) {
+ "Override Actor#update! (delta = %.2f)" printfln(delta)
+ }
+
+}
+
+ActorClosure: class extends Actor {
+
+ f: Func (Float)
+
+ init: func (=f) {
+
+ }
+
+ update: func (delta: Float) {
+ f(delta)
+ }
+
+}
+
View
@@ -0,0 +1,97 @@
+
+import Math
+
+// A box - pos is the top-left corner
+// Y+ is down, X+ is right
+Box: class {
+
+ pos: Vec2
+ width, height: Int
+
+ init: func (=pos, =width, =height) {
+ }
+
+ collide: func (other: Box) -> Bang {
+ x1 := pos x
+ y1 := pos y
+ minx1 := x1
+ maxx1 := x1 + width
+ miny1 := y1
+ maxy1 := y1 + height
+
+ x2 := other pos x
+ y2 := other pos y
+ minx2 := x2
+ maxx2 := x2 + other width
+ miny2 := y2
+ maxy2 := y2 + other height
+
+ // rule out quick cases first
+ if (maxx1 < minx2) return null
+ if (maxx2 < minx1) return null
+ if (maxy1 < miny2) return null
+ if (maxy2 < miny1) return null
+
+ bangs := false
+ b := Bang new()
+ b depth = 10000000000.0
+
+ if (x1 < x2 && maxx1 > minx2) {
+ depth := maxx1 - minx2
+ if (depth < b depth) {
+ bangs = true
+ b depth = depth
+ (b dir x, b dir y) = (-1, 0)
+ }
+ }
+
+ if (x2 < x1 && maxx2 > minx1) {
+ depth := maxx2 - minx1
+ if (depth < b depth) {
+ bangs = true
+ b depth = depth
+ (b dir x, b dir y) = ( 1, 0)
+ }
+ }
+
+ if (y1 < y2 && maxy1 > miny2) {
+ depth := maxy1 - miny2
+ if (depth < b depth) {
+ bangs = true
+ b depth = depth
+ (b dir x, b dir y) = ( 0, -1)
+ }
+ }
+
+ if (y2 < y1 && maxy2 > miny1) {
+ depth := maxy2 - miny1
+ if (depth < b depth) {
+ bangs = true
+ b depth = depth
+ (b dir x, b dir y) = ( 0, 1)
+ }
+ }
+
+ bangs ? b : null
+ }
+
+}
+
+Bang: class {
+
+ pos := vec2(0, 0)
+ dir := vec2(0, 1) // unit vector
+ depth := 0.0 // might be negative
+ other: Box
+
+ toString: func -> String {
+ "bang: pos %s, dir %s, depth %.2f" format(pos _, dir _, depth)
+ }
+
+ _: String { get {
+ toString()
+ }}
+
+}
+
+
View
@@ -1,5 +1,5 @@
-import UI, Timing
+import UI, Timing, Actor
import structs/[ArrayList]
@@ -10,6 +10,8 @@ Engine: class {
ui: UI
+ actors := ArrayList<Actor> new()
+
FPS := 30.0 // let's target 30FPS
init: func(config: ZombieConfig) {
@@ -25,6 +27,7 @@ Engine: class {
ticks = LTime getTicks()
ui update()
+ actors each(|a| a update(delta))
// teleport ourselves in the future when the next frame is due
roadToFuture := ticks + delta - LTime getTicks()
@@ -34,6 +37,14 @@ Engine: class {
}
}
+ add: func (actor: Actor) {
+ actors add(actor)
+ }
+
+ onTick: func (f: Func (Float)) {
+ actors add(ActorClosure new(f))
+ }
+
}
View
@@ -85,6 +85,11 @@ Vec2: class {
y += v y
}
+ add!: func ~floats (px, py: Float) {
+ x += px
+ y += py
+ }
+
perp: func -> This {
new(y, -x)
}
View
@@ -9,6 +9,7 @@ import Pass
import deadlogger/Log
import zombieconfig
import os/Time
+import sdl/Sdl
UI: class {
@@ -139,9 +140,14 @@ UI: class {
// it's a better practice to turn on debug locally
//input debug = true
+ input onKeyPress(Keys ESC, ||
+ SDL quit()
+ exit(0)
+ )
+
input onExit(||
- // just exit bluntly. Let's rely on the OS to free all the resources :D
- exit(0)
+ SDL quit()
+ exit(0)
)
}

0 comments on commit 4016257

Please sign in to comment.