Author Topic: Custom tileshape  (Read 10708 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Custom tileshape
« Reply #15 on: 2008-01-25, 02:05:16 PM »
this tile works now fine. i tried to make TopHalf, RightHalf and LeftHalf. but it seems i do just not understand enough. now i've realized that when there is no 0; there is a function. but i don't know what the "?" means and how it is with MinValue and MaxValue. writing functions is not easy when you don't know what you do.
if you don't mind, could you please give me an example for TopHalf?  :hurt:

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Custom tileshape
« Reply #16 on: 2008-01-25, 10:18:07 PM »
Yes, the ternary operator confuses a lot of people who are new to coding.  It's like "Where the heck did that come from?"

Basically the '?' is the first half of the ternary operator and the ':' is the second half. It works something like this:

boolean ? expression_1 : expression_2

Which evaluates to expression_1 if boolean is true, or expression_2 if boolean is false.  It's really just shorthand in most cases.  For example,

return (max >= height / 2 ? (short)(width-1) : short.MinValue);

either returns (short)(width-1) if max is greater than or equal to half of height, or returns short.MinValue otherwise.  It could be written as
Code: [Select]
if (max >= height / 2)
   return (short)(width-1);
else
   return short.MinValue;
but it's sometimes simpler to write things like this on one line without needing to write unnecessary code.

Here's a sample for TopHalf (not tested).
Code: (TopHalf) [Select]
public class TopHalf : TileShape
{
   private static TopHalf m_Value = new TopHalf();

   public static TopHalf Value
   {
      get
      {
         return m_Value;
      }
   }

   public TopHalf()
   {
   }

   public override short GetTopSolidPixel(short width, short height, short min, short max)
   {
      return (short)0;
   }

   public override short GetBottomSolidPixel(short width, short height, short min, short max)
   {
      return (short)(height/2);
   }

   public override short GetLeftSolidPixel(short width, short height, short min, short max)
   {
      return (max < height / 2 ? (short)0 : short.MaxValue);
   }

   public override short GetRightSolidPixel(short width, short height, short min, short max)
   {
      return (max < height / 2 ? (short)(width-1) : short.MaxValue);
   }
}
Edward Dassmesser