Noah Petherbridge
6af60f1128
* Improve the collision detection algorithm so that Actor OnCollide scripts get called more often WHILE an actor is moving, to prevent a fast-moving actor from zipping right through the "solid" hitbox and not giving the subject actor time to protest the movement. * It's implemented by adding a `Settled` boolean to the OnCollide event object. When the game is testing out movement, Settled=false to give the actor a chance to say "I'm solid!" and have the moving party be stopped early. * After all this is done, for any pair of actors still with overlapping hitboxes, OnCollide is called one last time with Settled=true. This is when the actor should run its actions (like publishing messages to other actors, changing state as in a trapdoor, etc.) * The new collision detection algorithm works as follows: * Stage 1 is the same as before, all mobile actors are moved and tested against level geometry. They record their Original and New position during this phase. * Stage 2 is where we re-run that movement but ping actors being intersected each step of the way. We trace the steps between Original and New position, test OnCollide handler, and if it returns false we move the mobile actor to the Last Good Position along the trace. * Stage 3 we run the final OnCollide(Settled=true) to let actors run actions they wanted to for their collide handler, WITHOUT spamming those actions during Stage 2. * This should now allow for tweaking of gravity speed and player speed without breaking all actor collision checking.
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package scripting
|
|
|
|
import (
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
|
"github.com/robertkrimen/otto"
|
|
)
|
|
|
|
// Message holds data being published from one script VM with information sent
|
|
// to the linked VMs.
|
|
type Message struct {
|
|
Name string
|
|
Args []interface{}
|
|
}
|
|
|
|
/*
|
|
RegisterPublishHooks adds the pub/sub hooks to a JavaScript VM.
|
|
|
|
This adds the global methods `Message.Subscribe(name, func)` and
|
|
`Message.Publish(name, args)` to the JavaScript VM's scope.
|
|
*/
|
|
func RegisterPublishHooks(vm *VM) {
|
|
// Goroutine to watch the VM's inbound channel and invoke Subscribe handlers
|
|
// for any matching messages received.
|
|
go func() {
|
|
for msg := range vm.Inbound {
|
|
log.Warn("vm: %s msg: %+v", vm.Name, msg)
|
|
if _, ok := vm.subscribe[msg.Name]; ok {
|
|
for _, callback := range vm.subscribe[msg.Name] {
|
|
callback.Call(otto.Value{}, msg.Args...)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Register the Message.Subscribe and Message.Publish functions.
|
|
vm.vm.Set("Message", map[string]interface{}{
|
|
"Subscribe": func(name string, callback otto.Value) {
|
|
if !callback.IsFunction() {
|
|
log.Error("SUBSCRIBE(%s): callback is not a function", name)
|
|
return
|
|
}
|
|
if _, ok := vm.subscribe[name]; !ok {
|
|
vm.subscribe[name] = []otto.Value{}
|
|
}
|
|
|
|
vm.subscribe[name] = append(vm.subscribe[name], callback)
|
|
},
|
|
|
|
"Publish": func(name string, v ...interface{}) {
|
|
log.Error("PUBLISH: %s %+v", name, v)
|
|
for _, channel := range vm.Outbound {
|
|
channel <- Message{
|
|
Name: name,
|
|
Args: v,
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|