making a slice bounded by/in another slice

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

Moderators: marionline, SDHawk

Post Reply
User avatar
SwordPlay
Chemical Slime
Posts: 805
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

making a slice bounded by/in another slice

Post by SwordPlay »

is there any way of ensuring that a slice can be moved, but only within the bounds of its parent?
Is there any way of allowing slices to move as long as they are touching or within another slice?
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

I'm guessing you'd check collisions to see if the slice is "within" the other one. There might be a better method, but that's the one that springs to mind. You'd check if they're colliding, and if they are, you allow movement. If they're not colliding, you prevent the movement. Something like.

GET INPUT

MOVE DIRECTION := INPUT DIRECTION

IF COLLIDING THEN

NEW LOCATION := OLD LOCATION + MOVE DIRECTION

ELSE

DO NOTHING

EN IF



Or something like that.
Last edited by BMR on Tue May 02, 2017 2:39 am, edited 2 times in total.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Use 'slice collide' to check if two slices intersect. Use 'slice contains' to check if one slice is entirely within another. You can also use 'clamp slice' to move a slice so that it's contained within another.

So, if you want to use normal slice movement commands, you could follow them with a while loop that checks whether the slice is still within the desired bounds, using slice contains/slice collide, and then call 'stop slice' when it leaves those bounds. Unfortunately that will leave it one step outside the bounds you want. You would have to manually move the slices, like BMR wrote, to avoid that.
User avatar
SwordPlay
Chemical Slime
Posts: 805
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

cheers guys.
What's "clamp slice"? I don't think I noticed that before? How does it work (roughly)?
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

docs!

Basically it changes the position of a slice so that it sits within (on top or behind) another slice (by screen coordinates), by separately restricting x and y coordinates.
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

I made a little demo of the "clamp slice" command.

Code: Select all


global variable(1, sprite)
global variable(2, box)
global variable(3, button)

plotscript, clampslice example, begin
  suspend player
  init mouse
  unhide mouse cursor

  load slice collection(0)
  sprite := lookup slice(sli:dinosaur)
  box := lookup slice(sli:box)
  button := lookup slice(sli:button)

  while(true) do(
    if(keyval(key:esc) > 1) then(game over)
    update button()
    set slice edge x(sprite, edge:center, mouse pixel x -- slice x(box))
    set slice edge y(sprite, edge:center, mouse pixel y -- slice y(box))
    clamp slice(sprite, box)
    wait(1)
  )
end

script, update button, begin
  if(slice collide point(button, mouse pixel x, mouse pixel y)) then(
    set rect bg col(button, random(0, 255))
    if(mouse click(left button)) then(
      randomize the box()
    )
  )else(
    set rect bg col(button, 96)
  )
end

script, randomize the box, begin
  variable(width)
  width := slice width(sprite layer)
  variable(height)
  height := slice height(sprite layer)
  set slice x(box, random(0, width / 4))
  set slice y(box, random(0, height / 4))
  set slice width(box, random(width / 2, width * 3/4))
  set slice height(box, random(height / 2, height * 3/4))
end
You can download the example rpg file here: https://www.slimesalad.com/forum/viewgame.php?p=128478

Image
User avatar
SwordPlay
Chemical Slime
Posts: 805
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

that's amazing! I was wondering how to do something like that for ages :p
(I have lots of ideas for slice-based games)
Post Reply