Skip to content

Instantly share code, notes, and snippets.

@200sc
Last active June 25, 2017 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 200sc/4787f9f47fb435d5193e5645ab9f2dcb to your computer and use it in GitHub Desktop.
Save 200sc/4787f9f47fb435d5193e5645ab9f2dcb to your computer and use it in GitHub Desktop.
package main
// This is a gist copy of https://github.com/200sc/collision-demo/blob/master/core.go
import (
"image/color"
"bitbucket.org/oakmoundstudio/oak"
"bitbucket.org/oakmoundstudio/oak/collision"
"bitbucket.org/oakmoundstudio/oak/entities"
"bitbucket.org/oakmoundstudio/oak/event"
"bitbucket.org/oakmoundstudio/oak/render"
)
const (
_ = iota
RED collision.Label = iota
GREEN
BLUE
TEAL
)
func main() {
oak.AddScene("demo", func(string, interface{}) {
act := &AttachCollisionTest{}
act.Solid = entities.NewSolid(50, 50, 50, 50, render.NewColorBox(50, 50, color.RGBA{0, 0, 0, 255}), act.Init())
collision.Attach(act.Vector, act.Space, 0, 0)
act.Bind(func(int, interface{}) int {
if act.ShouldUpdate {
act.ShouldUpdate = false
act.R.UnDraw()
act.R = act.nextR
render.Draw(act.R, 0)
}
if oak.IsDown("A") {
// We could use attachment here to not have to shift both
// R and act but that is made more difficult by constantly
// changing the act's R
act.ShiftX(-3)
act.R.ShiftX(-3)
} else if oak.IsDown("D") {
act.ShiftX(3)
act.R.ShiftX(3)
}
if oak.IsDown("W") {
act.ShiftY(-3)
act.R.ShiftY(-3)
} else if oak.IsDown("S") {
act.ShiftY(3)
act.R.ShiftY(3)
}
return 0
}, "EnterFrame")
render.Draw(act.R, 0)
act.R.SetLayer(1)
collision.PhaseCollision(act.Space)
act.Bind(func(id int, label interface{}) int {
l := label.(collision.Label)
switch l {
case RED:
act.r += 125
act.UpdateR()
case GREEN:
act.g += 125
act.UpdateR()
case BLUE:
act.b += 125
act.UpdateR()
case TEAL:
act.b += 125
act.g += 125
act.UpdateR()
}
return 0
}, "CollisionStart")
act.Bind(func(id int, label interface{}) int {
l := label.(collision.Label)
switch l {
case RED:
act.r -= 125
act.UpdateR()
case GREEN:
act.g -= 125
act.UpdateR()
case BLUE:
act.b -= 125
act.UpdateR()
case TEAL:
act.b -= 125
act.g -= 125
act.UpdateR()
}
return 0
}, "CollisionStop")
upleft := entities.NewSolid(0, 0, 320, 240, render.NewColorBox(320, 240, color.RGBA{100, 0, 0, 100}), 0)
upleft.Space.UpdateLabel(RED)
upleft.R.SetLayer(0)
render.Draw(upleft.R, 0)
upright := entities.NewSolid(320, 0, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 100, 0, 100}), 0)
upright.Space.UpdateLabel(GREEN)
upright.R.SetLayer(0)
render.Draw(upright.R, 0)
botleft := entities.NewSolid(0, 240, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 0, 100, 100}), 0)
botleft.Space.UpdateLabel(BLUE)
botleft.R.SetLayer(0)
render.Draw(botleft.R, 0)
botright := entities.NewSolid(320, 240, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 100, 100, 100}), 0)
botright.Space.UpdateLabel(TEAL)
botright.R.SetLayer(0)
render.Draw(botright.R, 0)
}, func() bool {
return true
}, func() (string, *oak.SceneResult) {
return "demo", nil
})
render.SetDrawStack(
render.NewHeap(false),
render.NewDrawFPS(),
)
oak.Init("demo")
}
type AttachCollisionTest struct {
entities.Solid
// AttachSpace is a composable struct that allows
// spaces to be attached to vectors
collision.AttachSpace
// Phase is a composable struct that enables the call
// collision.CollisionPhase on this struct's space,
// which will start sending signals when that space
// starts and stops touching given labels
collision.Phase
r, g, b int
ShouldUpdate bool
nextR render.Renderable
}
func (act *AttachCollisionTest) Init() event.CID {
cid := event.NextID(act)
act.CID = cid
return cid
}
func (act *AttachCollisionTest) UpdateR() {
act.nextR = render.NewColorBox(50, 50, color.RGBA{uint8(act.r), uint8(act.g), uint8(act.b), 255})
act.nextR.SetPos(act.X(), act.Y())
act.nextR.SetLayer(1)
act.ShouldUpdate = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment