Adjust Gravity and Prevent Moonwalking

* Tweak max gravity speed to match player max velocity.
* Boy's script watches for his velocity to flip suddenly and stops
  animations, limiting the moonwalking a bit.
* JS API: Self.GetVelocity() added.
This commit is contained in:
Noah 2021-10-07 18:49:09 -07:00
parent fb5a8a1ae8
commit d6acee5a66
4 changed files with 14 additions and 4 deletions

View File

@ -1,11 +1,9 @@
function main() { function main() {
var playerSpeed = 12; var playerSpeed = 12;
var gravity = 4;
var Vx = Vy = 0; var Vx = Vy = 0;
var animating = false; var animating = false;
var animStart = animEnd = 0; var animStart = animEnd = 0;
var animFrame = animStart;
Self.SetMobile(true); Self.SetMobile(true);
Self.SetInventory(true); Self.SetInventory(true);
@ -14,10 +12,20 @@ function main() {
Self.AddAnimation("walk-left", 200, ["stand-left", "walk-left-1", "walk-left-2", "walk-left-3", "walk-left-2", "walk-left-1"]); Self.AddAnimation("walk-left", 200, ["stand-left", "walk-left-1", "walk-left-2", "walk-left-3", "walk-left-2", "walk-left-1"]);
Self.AddAnimation("walk-right", 200, ["stand-right", "walk-right-1", "walk-right-2", "walk-right-3", "walk-right-2", "walk-right-1"]); Self.AddAnimation("walk-right", 200, ["stand-right", "walk-right-1", "walk-right-2", "walk-right-3", "walk-right-2", "walk-right-1"]);
// If the player suddenly changes direction, reset the animation state to quickly switch over.
var lastVelocity = Vector(0, 0);
Events.OnKeypress(function (ev) { Events.OnKeypress(function (ev) {
Vx = 0; Vx = 0;
Vy = 0; Vy = 0;
var curVelocity = Self.GetVelocity();
if ((lastVelocity.X < 0 && curVelocity.X > 0) ||
(lastVelocity.X > 0 && curVelocity.X < 0)) {
Self.StopAnimation();
}
lastVelocity = curVelocity;
if (ev.Right) { if (ev.Right) {
if (!Self.IsAnimating()) { if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-right", null); Self.PlayAnimation("walk-right", null);

View File

@ -26,7 +26,7 @@ var (
PlayerMaxVelocity float64 = 7 PlayerMaxVelocity float64 = 7
PlayerJumpVelocity float64 = -20 PlayerJumpVelocity float64 = -20
PlayerAcceleration float64 = 0.12 PlayerAcceleration float64 = 0.12
Gravity float64 = 6 Gravity float64 = 7
GravityAcceleration float64 = 0.1 GravityAcceleration float64 = 0.1
SlopeMaxHeight = 8 // max pixel height for player to walk up a slope SlopeMaxHeight = 8 // max pixel height for player to walk up a slope

View File

@ -31,7 +31,8 @@ func (s *PlayScene) LoopTouchable(ev *event.State) {
// Idle means that they are not holding any directional or otherwise input key. // Idle means that they are not holding any directional or otherwise input key.
// Keyboard inputs and touch events from this function will set these keys. // Keyboard inputs and touch events from this function will set these keys.
// See if it stays unset long enough to consider idle. // See if it stays unset long enough to consider idle.
if !ev.Up && !ev.Down && !ev.Right && !ev.Left && !ev.Space { var isGrounded = (s.Player.HasGravity() && s.Player.Grounded()) || !s.Player.HasGravity()
if !ev.Up && !ev.Down && !ev.Right && !ev.Left && !ev.Space && isGrounded {
if s.idleLastStart.IsZero() { if s.idleLastStart.IsZero() {
s.idleLastStart = time.Now() s.idleLastStart = time.Now()
} else if time.Since(s.idleLastStart) > balance.PlayModeIdleTimeout { } else if time.Since(s.idleLastStart) > balance.PlayModeIdleTimeout {

View File

@ -26,6 +26,7 @@ func (w *Canvas) MakeSelfAPI(actor *Actor) map[string]interface{} {
"SetHitbox": actor.SetHitbox, "SetHitbox": actor.SetHitbox,
"Hitbox": actor.Hitbox, "Hitbox": actor.Hitbox,
"SetVelocity": actor.SetVelocity, "SetVelocity": actor.SetVelocity,
"GetVelocity": actor.Velocity,
"SetMobile": actor.SetMobile, "SetMobile": actor.SetMobile,
"SetInventory": actor.SetInventory, "SetInventory": actor.SetInventory,
"HasInventory": actor.HasInventory, "HasInventory": actor.HasInventory,