Jump to content

  • Log In with Google      Sign In   
  • Create Account

MrZeo

Member Since 10 Apr 2013
Offline Last Active Apr 24 2013 01:04 AM

Posts I've Made

In Topic: Help with Worm Clone Draw() method

24 April 2013 - 01:02 AM

Its hard to tell, maybe you are incorrectly substracting 1 when looping through your body parts in draw?

 

for (int i = 0; i < wormBodyTexture.Count; i++)

 

Have you considered making a simple BodyPart class, and making a list of those that would represent the worm. First object in the list would be the head and last one the tail. When the worm moves you would just remove the last item from the list and insert new one at the start of the list based on direction you are going. When you eat something you just dont remove the last item and thus the worm gets larger. For collision you can easily check if the new head position collides with any other BodyPart in the list or walls of the map. When drawing you would just loop through the list, drawing the first part with head texture, last with tail, and the rest with body texture. Later on when you have the basics down you could easily expand the class to do all kinds of neat stuff. I hope that wasn't too confusing. :>


In Topic: Help with Worm Clone

19 April 2013 - 02:23 PM

Vector2 is a struct and therefore a value type. When you return value from list you actually get a copy of the value instead of reference. That is why you can't modify the original value in list.

 

EDIT: This should work not sure if it is a good way to do it though:

 

var temp = wormBodyPosition[i];

temp.X += wormBodyWidth;

wormBodyPosition[i] = temp;


In Topic: Reading array of Rectangles from XML

19 April 2013 - 01:57 PM

See http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx - Everything you ever wanted to know about IntermediateSerializer.

That's an optimization applied to certain types of arrays.

 

Thank you, excactly the information I needed, the built in serializer isn't very well documented.

 

Now for example I'm deserializing class something like this:

public class SomeDataList
{
    [ContentSerializer]
    private SomeData[] data;

    // public properties and other stuff.

    public SomeDataList()
    {}

    public SomeData GetSomeData(int index)
    {
        // return stuff.
    }
}

 

If I understood correctly the serializer requires parameterless constructor. Can I just mark this constructor private to only ever create instances of the class during content loading?

 

Also just to clarify I am correct on this right?

// Both reference the same object.
SomeDataList listFirst = Content.Load<SomeDataList>("ListOne");
SomeDataList listSecond = Content.Load<SomeDataList>("ListOne");

// Two different objects.
SomeDataList listFirst = Content.Load<SomeDataList>("ListOne");
SomeDataList listSecond = Content.Load<SomeDataList>("ListTwo");

 

Thanks for the help again. :>


In Topic: Drawing large tile maps

13 April 2013 - 09:38 PM

Thx for all the replies! I have to be able to interact with the map runtime so I don't think splitting it to textures would work. I was able to make some progress though. I dropped the whole idea of separate tile class and instead have the data in simple byte[x,y,z] array. I'm not really sure what was causing the slowdown before but now I can run steady 60 fps as long as I don't zoom too far out.

 

I probably have to change to SpriteSortMode.BackToFront so that I can utilize layerdepth for sorting. Even though I'm already naturally sorting the map itself, I have to somehow get depth sorted with moving objects that might be behind parts of the map.

 

Here is the draw/culling, not pretty but does the job:

 

        public void Draw(SpriteBatch spriteBatch)
        {
            // Start tile of horizontal row to be drawn.
            int dx = topLeft.X;
            int dy = topLeft.Y;

            // Tile to be drawn.
            int x, y;

            bool swap = true;

            spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, camera.GetViewMatrix());

            do
            {
                // Update tile to start coordinates of new row.
                x = dx;
                y = dy;

                do
                {
                    // Make sure we are within map limits.
                    if (x >= 0 && x < Map.X && y >= 0 && y < Map.Y)
                    {
                        for (int z = 0; z < Map.Z; z++)
                        {
                            if (data[x, y, z] > 0)
                            {
                                tilePosition = IsoTransform(x, y);
                                GetTileData(data[x, y, z], z, ref sourceRect, ref tileAlpha);
                                spriteBatch.Draw(tileSheet, tilePosition, sourceRect, tileColor * tileAlpha, 0, tileOrigin, 1f, SpriteEffects.None, 0);
                            }
                        }
                    }

                    // Add to draw our next horizontal tile.
                    x++; y--;

                } while (x <= dx + maxX); // Horizontal row done.

                // Add to either dx or dy to get to the next row.
                if (swap)
                {
                    dx += 1;
                    swap = false;
                }
                else
                {
                    dy += 1;
                    swap = true;
                }
            } while (dy <= topLeft.Y + maxY); // We are done drawing once we reach maxY.

            spriteBatch.End();
        }

        private void UpdateLimits()
        {
            // Get view rect coordinates.
            Vector2 topLeft = Vector2.Transform(new Vector2(0, 0), camera.InverseViewMatrix);
            Vector2 bottomRight = Vector2.Transform(screenSize, camera.InverseViewMatrix);

            // Calculate how many tiles fit in to this view.
            maxX = (int)(bottomRight.X - topLeft.X) / Tile.X;
            maxY = (int)(bottomRight.Y - topLeft.Y) / Tile.Y;

            // Get first tile to be drawn.
            this.topLeft = WorldToTile(topLeft, false);

            // Add a bit extra around the view to make sure half tiles get drawn.
            maxX += 3;
            maxY += 3;
            this.topLeft.X -= 2;
        }

 


PARTNERS