Jump to content


Photo
- - - - -

A Simple Trick To Orientating Blocks


  • Please log in to reply
9 replies to this topic

#1 Blake

Blake

    GMC Member

  • GMC Member
  • 508 posts
  • Version:GM:Studio

Posted 21 February 2012 - 07:25 AM

  • Title: A Simple Trick To Orientating Blocks
  • Description: Automatically orientates your blocks depending on the surrounding blocks.
  • GM Version: GM8
  • Registered: No
  • File Type: .gmk
  • File Size: 13.3kb
  • File Link: Download
  • Required Extensions: None
  • Required DLLs: None
Summary

When creating platform or top-down games, you often want your blocks (or platforms or walls etc.) to show different sprites or tiles depending on the surrounding blocks. Some solutions to this problem that I have seen involve essays of code that repeat place_meeting functions over and over for every different possibility of surrounding blocks. With this simple trick that I will show you though, this is not necessary. If we arrange the sprite subimages in a particular order, we can then check all four sides of each block for another block, and increase the image_index by certain amounts depending on the surrounding block positions, and this results in the correct subimage. Similarly, if we arrange the tiles in a particular horizontal order, and instead increase a variable by certain amounts depending on the surrounding block positions, we achieve the correct tile. This may be difficult to comprehend in writing, so take a look at the following scripts and see if that helps to make it more clear.

The .gmk file shows how to achieve this with both sprites and tiles. You are free to use the scripts/example in your game. When doing so, please note that in order for it to work correctly your sprite subimages must be in the same order as mine (if using sprites) and your tiles must be in the same order as mine and in one single horizontal row (if using tiles).

For sprites, we can use the following script:

//scr_orientate_sprite(objectname,spritewidth,spriteheight);
name = argument0;
width = argument1;
height = argument2;

image_speed = 0;
image_index = 0;

if place_meeting(x-width,y,name) image_index += 8;
if place_meeting(x+width,y,name) image_index += 4;
if place_meeting(x,y-height,name) image_index += 2;
if place_meeting(x,y+height,name) image_index += 1;

For tiles, we can use the following script:

//scr_orientate_tile(objectname,backgroundname,tilewidth,tileheight);

name = argument0;
bg = argument1;
width = argument2;
height = argument3;
count = 0;

if place_meeting(x-width,y,name) count += 8;
if place_meeting(x+width,y,name) count += 4;
if place_meeting(x,y-height,name) count += 2;
if place_meeting(x,y+height,name) count += 1;

tile_add(bg,width*count,0,width,height,x,y,depth);

Here is a screenshot of the example in action. On the left we have sprites and on the right we have tiles.

Posted Image

I hope this example is useful! (It better be... it took so damn long to put together! Oh, what the hell, I have no life anyway :tongue:)
  • 2

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Member
  • 15888 posts
  • Version:GM:Studio

Posted 21 February 2012 - 11:27 PM

We have a few of those, but this has a tile method. Very nice.
  • 0

#3 @Alex@

@Alex@

    GMC Member

  • GMC Member
  • 4198 posts
  • Version:Unknown

Posted 22 February 2012 - 12:05 AM

I'd hate expanding this for smooth blends of terrain types. The combinations grow far to quickly for my tastes, then again it would save a bunch of time on a large scale project. Nice and simple example.
  • 0

#4 Thagrahn

Thagrahn

    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 27 February 2012 - 05:32 AM

Seems to be designed around being just one tile or sprite set of 16 images, so seems to have limits on what it can handle.

I thing having to draw out and figure out the number of tiles and images for other types of things would take a lot longer to begin with.


One thing that would help this go further would be if there was a way to apply tile sets to multiple layers, and only check that layer.

Other wise, one tile set and several terrain sprites would be needed.


Example Idea: Lets say you wanted to do a room with an autumn theme.

First would be the grass tile that actually covers the full room.
Second would be a tile set for a different type of grass.

If you couldn't use tile sets on other layers, you would have to use sprites with high layer numbers for things like the Leaves, Paths, and Dirt.
  • 0

#5 Desert Dog

Desert Dog

    GMC Member

  • GMC Member
  • 6409 posts
  • Version:Unknown

Posted 27 February 2012 - 06:38 AM

Good job on this, thanks for sharing.
  • 0

#6 Blake

Blake

    GMC Member

  • GMC Member
  • 508 posts
  • Version:GM:Studio

Posted 27 February 2012 - 07:13 AM

Thanks for all the positive comments! :happy:

I'm well aware that there is limitations to this with regard to having more than 16 subimages in order to achieve more smooth edges etc. I've thought about expanding this to cover that, but to be honest, a grand total number of downloads that barely scrapes into the double figures isn't enough motivation for me :tongue:
  • 0

#7 icymx

icymx

    Patrick Brett

  • GMC Member
  • 800 posts
  • Version:GM8

Posted 18 March 2012 - 03:03 AM

Worked great for me. Thanks Blake! :D
  • 0

#8 The Rock Abdo

The Rock Abdo

    GMC Member

  • GMC Member
  • 58 posts
  • Version:GM:Studio

Posted 02 April 2012 - 10:28 AM

great tutorial .. thanks :thumbsup:
  • 0

#9 CptGT99

CptGT99

    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 25 April 2012 - 03:50 PM

works good, thanks!
  • 0

#10 trianglegames

trianglegames

    GMC Member

  • GMC Member
  • 36 posts
  • Version:Mac

Posted 16 May 2012 - 11:22 PM

I'm gonna try it out!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users