Pub/Sub Messages Between Linked Actors (JavaScript)

* Implement the pub/sub message passing system that lets the JavaScript
  VM of one actor (say, a Button) send messages to other linked actors
  in the level (say, an Electric Door)
* Buttons now emit a "power(true)" message while pressed and
  "power(false)" when released. Sticky Buttons do not release and so do
  not send the power(false) message.
* Electric Doors listen for the "power" event and open or close
  themselves based on the boolean value received.
* If a Sticky Button receives power and is currently pressed down, it
  will pop back up (reset to "off" position) and notify its linked
  actors that they have lost power too. So if a Sticky Button held an
  Electric Door open, and another Button powers the Sticky Button, it
  would pop back up and also close the Electric Door.
This commit is contained in:
Noah 2019-06-23 17:30:12 -07:00
parent a1a5217309
commit f0393fb4c7
5 changed files with 50 additions and 25 deletions

View File

@ -1,9 +1,6 @@
function main() {
log.Info("Azulian '%s' initialized!", Self.Doodad.Title);
Self.Canvas.SetBackground(RGBA(0, 153, 255, 100));
var playerSpeed = 12;
var gravity = 4;
var Vx = Vy = 0;

View File

@ -6,11 +6,10 @@ function main() {
Events.OnCollide(function(e) {
// Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) {
Self.Canvas.SetBackground(RGBA(0, 255, 0, 153));
return;
}
Self.Canvas.SetBackground(RGBA(255, 255, 0, 153));
Message.Publish("power", true);
if (timer > 0) {
clearTimeout(timer);
@ -19,12 +18,13 @@ function main() {
Self.ShowLayer(1);
timer = setTimeout(function() {
Self.ShowLayer(0);
Message.Publish("power", false);
timer = 0;
}, 200);
});
Events.OnLeave(function(e) {
console.log("%s has stopped touching %s", e, Self.Doodad.Title)
Self.Canvas.SetBackground(RGBA(0, 0, 1, 0));
})
// Events.OnLeave(function(e) {
// console.log("%s has stopped touching %s", e, Self.Doodad.Title)
// Self.Canvas.SetBackground(RGBA(0, 0, 1, 0));
// })
}

View File

@ -3,6 +3,15 @@ function main() {
var pressed = false;
// When a sticky button receives power, it pops back up.
Message.Subscribe("power", function(powered) {
if (powered && pressed) {
Self.ShowLayer(0);
pressed = false;
Message.Publish("power", false);
}
})
Events.OnCollide(function(e) {
if (pressed) {
return;
@ -15,5 +24,6 @@ function main() {
Self.ShowLayer(1);
pressed = true;
Message.Publish("power", true);
});
}

View File

@ -6,24 +6,42 @@ function main() {
var animating = false;
var opened = false;
Events.OnCollide(function(e) {
Self.SetHitbox(16, 0, 32, 64);
Message.Subscribe("power", function(powered) {
console.log("%s got power=%+v", Self.Doodad.Title, powered);
if (powered) {
if (animating || opened) {
return;
}
if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) {
animating = true;
Self.PlayAnimation("open", function() {
opened = true;
animating = false;
});
} else {
animating = true;
Self.PlayAnimation("close", function() {
opened = false;
animating = false;
})
}
});
Events.OnCollide(function(e) {
if (e.InHitbox) {
if (!opened) {
return false;
}
}
});
Events.OnLeave(function() {
if (opened) {
Self.PlayAnimation("close", function() {
opened = false;
});
}
// if (opened) {
// Self.PlayAnimation("close", function() {
// opened = false;
// });
// }
})
}

View File

@ -2,7 +2,7 @@ function main() {
Self.AddAnimation("open", 0, [1]);
var unlocked = false;
Self.Canvas.SetBackground(RGBA(0, 255, 255, 100));
// Self.Canvas.SetBackground(RGBA(0, 255, 255, 100));
// Map our door names to key names.
var KeyMap = {
@ -12,8 +12,8 @@ function main() {
"Yellow Door": "Yellow Key"
}
log.Warn("%s loaded!", Self.Doodad.Title);
console.log("%s Setting hitbox", Self.Doodad.Title);
// log.Warn("%s loaded!", Self.Doodad.Title);
// console.log("%s Setting hitbox", Self.Doodad.Title);
Self.SetHitbox(16, 0, 32, 64);
Events.OnCollide(function(e) {
@ -32,7 +32,7 @@ function main() {
Self.PlayAnimation("open", null);
}
});
Events.OnLeave(function(e) {
console.log("%s has stopped touching %s", e, Self.Doodad.Title)
})
// Events.OnLeave(function(e) {
// console.log("%s has stopped touching %s", e, Self.Doodad.Title)
// })
}