This is the pits.

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
Hedera
Slime Knight
Posts: 175
Joined: Tue May 17, 2011 11:38 am
Location: a dying forest (all forests are dying)

This is the pits.

Post by Hedera »

So. In my game, I want to have pits that the player can fall down, and for them to play a little falling animation. This effect can be done in multiple ways, many of which would lead to a Wile E. Coyote effect as the player only starts falling once they've fully walked onto the pit tile; while this would ultimately be acceptable, I'm wondering if it's possible for it to instead start the animation as the player starts walking into the pit (or is already partway through).

In descending order of complexity, the following features would be good to implement:

0 - Player walks into pit tile, screen goes black, sound effect plays, they land somewhere. The most basic pit, looking for something better than this.

1 - Player walks into pit tile, an animation of them falling down occurs, fade to black. Should be easy to do, and should be satisfactory.

2 - Player starts walking into the pit tile, animation starts playing when they're mid-way through the tile.

3 - As above, but plays a different animation depending on what direction they're heading as they walk into the pit. Should be easy to do, if you've created pit #2?

4 - As above, but the animation starts right as the player begins to walk into the pit tile. I imagine the script would have to be already active when the player's in a tile adjacent to the pit, and then checking for the keypress that'd send them into it.

There was a bit of discussion in the Q&A sub-discord on this; what's the etiquette for copying and pasting them to the forums?
Attachments
The Immortal .gif courtesy of i-mockery
The Immortal .gif courtesy of i-mockery
immortaldeaths06.gif (301.85 KiB) Viewed 5135 times
Last edited by Hedera on Wed Apr 21, 2021 3:15 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4310
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Checking the tile you're on once you're over halfway onto seems simple:

Code: Select all

if (read zone (ZONEID, (hero pixel x (me) + 10) / 20, (hero pixel y (me) + 10) / 20) then (...)
But actually the above code is not symmetric whether you're approaching from the left or right, because if you're exactly half-way between tiles (possible if the walk speed is 1, 5 or 10) then it conts as the tile to the right.

Getting to about 3.8 on your scale is the easiest place to stop: create an animation that triggers one tick after the hero begins to move onto the tile. If you really want to go all the way to 4 then you need to use the Hero will move script on the wiki. The script largely works but there are edgecases that break it, so I recommend against using it if you can avoid it. Most importantly it doesn't support mouse controls at all!

First some useful scripts:


So to figure out which tile the player is walking onto:

Code: Select all

if ((hero pixel x (me), mod, 20) || (hero pixel y (me), mod, 20)) then (
  # The hero is partway between tiles. Figure out the destination tile
  variable(x, y)
  x := hero pixel x (me) / 20  # Rounds to the tile to the left
  y := hero pixel y (me) / 20  # Rounds to the tile above
  if (hero direction(me) == down) then (y += 1)
  if (hero direction(me) == right) then (x += 1)
  if (read zone(ZONEID, x, y)) then (
    # Stepping onto a pit, start animation here
  )
)
You have to do this check every tick, using either a timer or a while loop, and stop checking once the animation has finished.
However if you used "hero will move" instead then you could just use an on-keypress script.

You can check the hero direction in the same way to decide which animation to play; animations are another subject.

EDIT (8/5/21: fixed bug)
Last edited by TMC on Fri May 07, 2021 1:00 pm, edited 2 times in total.
User avatar
Hedera
Slime Knight
Posts: 175
Joined: Tue May 17, 2011 11:38 am
Location: a dying forest (all forests are dying)

Post by Hedera »

Oh geez, I hadn't even taken mouse controls into consideration. 3.8 it is, then.
TMC
Metal King Slime
Posts: 4310
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Come to think of it, the script I posted could break when using mouse movement of multiple tiles at a time. I've edited my post to fix the bug. (It assumed that "heroiswalking(me)" is true only when you're already partway between tiles, but when making multitile movements you might be aligned to the tile grid.)
User avatar
Hedera
Slime Knight
Posts: 175
Joined: Tue May 17, 2011 11:38 am
Location: a dying forest (all forests are dying)

Post by Hedera »

TMC wrote:You have to do this check every tick, using either a timer or a while loop, and stop checking once the animation has finished.
However if you used "hero will move" instead then you could just use an on-keypress script.
(coming back to this after not doing any work for a couple weeks :???:)

Here's what I have so far: two scripts, the first being the one you did inside a while loop, and the second being a placeholder animation for actually falling down the pit and dying. test pit check is used as an autorun script for the maps that have pits. It doesn't seem to trigger the second one, though, or else it does and it's not doing anything. Or possibly both. I don't know.

Code: Select all

plotscript, test pit check, begin							# All credit to TMC for this script


	while (current map == (3 || 7))							# this script only runs on maps 3 and 7 at the moment, so it forms a tautology
	do	(
		if ((hero pixel x (me), mod, 20) || (hero pixel y (me), mod, 20))
		then	(
			variable(x, y)
			x := hero pixel x (me) / 20 
			y := hero pixel y (me) / 20 
			if (hero direction(me) == up) then (y += 1)
			if (hero direction(me) == right) then (x += 1)
			if (read zone((4 || 5 || 6), x, y))					# zone 4 is the pit proper, zones 5 and 6 are for disappearing pits
			then	(
				test pit fall
				)
			)
		)

	end


plotscript, test pit fall, begin


	set hero z (leader, -2)		# This is a temporary "fall down and die" "animation", and should be replaced asap
	wait (1)
	set hero z (leader, -10)
	wait (1)
	fade screen out (63, 0, 0)
	wait (1)
	game over
	
	end
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7684
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Code: Select all

read zone((4 || 5 || 6), x, y)
This definitely won't do what you expect. You need to change it to:

Code: Select all

read zone(4, x, y) || read zone(5, x, y) || read zone(6, x, y)
When you write (4 || 5 || 6) it treats them all as true/false values, and the combined result is true which is 1, so you actually only check zone 1
TMC
Metal King Slime
Posts: 4310
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Also, I noticed a bug in the script I gave you (which I didn't, and still haven't, tested):

Code: Select all

if (hero direction(me) == up) then (y += 1) 
should be

Code: Select all

if (hero direction(me) == down) then (y += 1) 
Last edited by TMC on Fri May 07, 2021 12:59 pm, edited 1 time in total.
User avatar
Hedera
Slime Knight
Posts: 175
Joined: Tue May 17, 2011 11:38 am
Location: a dying forest (all forests are dying)

Post by Hedera »

I got it to work! Thank you both for helping me with this.

After applying your bugfixes the while loop was causing the script to hang, until I remembered to put "else wait (1)" at the end of the ifs. Really need to make a note to myself to do that automatically.
Post Reply