2

I am trying to write a small game, it works pretty good for now, but I need something like a collision detection and for that detection it is important to know the excat size of that unicode character(may be converted to a graphic, to work better with it, instead of using a label ?!)

I thought it would be great, to get all the coordinates of that element where it is black and leave out where it is transparent.Maybe something based on this, but I am m not quite sure wether it is a good and fast approach. By the way it is important to get the coordinates based on that canvas.

What I have so far:

The unicode characters e.g.:

"\U0001F6B5"

The element for the canvas, I could also use a graphical object:

var customLabel = new Label
{
    Uid= all[i].ToString().Equals("Biker")?"Biker":"",
    TabIndex = i,
    Margin = new Thickness(0),
    Content = all[i].Icon,//"\U0001F6B5"
    BorderThickness = new Thickness(2.0),
    Name = controlName,
    FontSize = 22,
    Padding = new Thickness(0.0, 0.0, 0.0, 0.0),
    ToolTip = tooltip, //may be tooltip with real pictures from that object in real life and a "Registerkare" with all its properties(armor, power,...)
    //BorderBrush = Brushes.Red //to see the size of this element
    Cursor = Cursors.Hand
    // FlowDirection= //use if the object moves left, right, top, butoom ?
    //IsHitTestVisible= //use for collision ?

}; 

//Setting the position on the canvas:

Canvas.SetLeft(customLabel, xRef); //X
Canvas.SetTop(customLabel, yRef); //Y

At the moment I came up with that solution(seeing an element as a rectangle), I use it only once, when I create a random map(later the player will check against those saved positions, via "INotifyPropertyChanged" if his position changed):

for (int xx = 1; xx <= customLabel.Width; xx++)//range for object
{
    for (int yy = 1; yy <= customLabel.Height; yy++)//range for object
    {
        coordinatesInUse.Add(new KeyValuePair<int, int>((int)xRef+xx, (int)xRef+yy));
        var lookup = coordinatesInUse.ToLookup(kvp => kvp.Key, kvp => kvp.Value);

        foreach (int look in lookup[1])
        {
            //checking wether that element exist or not
        }
    }
}
Community
  • 1
  • 1
user254197
  • 883
  • 2
  • 9
  • 31
  • Why do you always want to create a new KeyValuePair collection? – Akansha Aug 11 '15 at 04:25
  • Because "coordinatesInUse" is declared like "List> coordinatesInUse;". Actually at the beginning I wanted to use a dictionary with a key(x-coordinates) and a value(y-coordinates), but the problem there was I could only use a single uniqe key, so I came up with that solution... I think it is way slower, than the dictionary collection.. – user254197 Aug 11 '15 at 04:29

1 Answers1

0

Create a custom class consisting of polygon(rectangle), x-cordinate and y-cordinate. Then List will be your collection of element,x and y coordinates put together. Instead of creating a new KeyValue Pair everytime, you can use the same collection all over and just change the values as needed.

Akansha
  • 933
  • 7
  • 18