Skip to content

Commit

Permalink
Actors, collisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Aug 22, 2012
1 parent c7daf4f commit 4016257
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
25 changes: 25 additions & 0 deletions source/ldkit/Actor.ooc
Original file line number Original file line Diff line number Diff line change
@@ -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)
}

}

97 changes: 97 additions & 0 deletions source/ldkit/Collision.ooc
Original file line number Original file line Diff line number Diff line change
@@ -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()
}}

}


13 changes: 12 additions & 1 deletion source/ldkit/Engine.ooc
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@


import UI, Timing import UI, Timing, Actor


import structs/[ArrayList] import structs/[ArrayList]


Expand All @@ -10,6 +10,8 @@ Engine: class {


ui: UI ui: UI


actors := ArrayList<Actor> new()

FPS := 30.0 // let's target 30FPS FPS := 30.0 // let's target 30FPS


init: func(config: ZombieConfig) { init: func(config: ZombieConfig) {
Expand All @@ -25,6 +27,7 @@ Engine: class {
ticks = LTime getTicks() ticks = LTime getTicks()


ui update() ui update()
actors each(|a| a update(delta))


// teleport ourselves in the future when the next frame is due // teleport ourselves in the future when the next frame is due
roadToFuture := ticks + delta - LTime getTicks() roadToFuture := ticks + delta - LTime getTicks()
Expand All @@ -34,6 +37,14 @@ Engine: class {
} }
} }


add: func (actor: Actor) {
actors add(actor)
}

onTick: func (f: Func (Float)) {
actors add(ActorClosure new(f))
}

} }




5 changes: 5 additions & 0 deletions source/ldkit/Math.ooc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ Vec2: class {
y += v y y += v y
} }


add!: func ~floats (px, py: Float) {
x += px
y += py
}

perp: func -> This { perp: func -> This {
new(y, -x) new(y, -x)
} }
Expand Down
10 changes: 8 additions & 2 deletions source/ldkit/UI.ooc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Pass
import deadlogger/Log import deadlogger/Log
import zombieconfig import zombieconfig
import os/Time import os/Time
import sdl/Sdl


UI: class { UI: class {


Expand Down Expand Up @@ -139,9 +140,14 @@ UI: class {
// it's a better practice to turn on debug locally // it's a better practice to turn on debug locally
//input debug = true //input debug = true


input onKeyPress(Keys ESC, ||
SDL quit()
exit(0)
)

input onExit(|| input onExit(||
// just exit bluntly. Let's rely on the OS to free all the resources :D SDL quit()
exit(0) exit(0)
) )
} }


Expand Down

0 comments on commit 4016257

Please sign in to comment.