Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter way to move NSView bounds with player?
    primarykey
    data
    text
    <p>Currently writing a roguelike to learn more about Objective-C/Cocoa. I'm really enjoying it so far and I've learned tons. </p> <p>This code moves the <code>origin</code> of the view's <code>bounds</code>, so that it follows the player as he moves. The code works perfect, I was just asking if there was a better way than using four <code>for</code>'s.</p> <p>I've also seen in some cases that it's better to do things separately instead of all in one, especially where drawing and <code>nsbezierpath</code> is concerned, why is that?</p> <p><strong>Edit</strong>: People were having trouble figuring out exactly what I'm doing, so I'm going to break it down as much as I can.</p> <p>The <code>view</code> is a 30x30 grid (0-29,0-29) of tiles, 20x20 each. The map can be as big or small as needs to be.</p> <p>First, you get the <code>[player_ location]</code>, along with the <code>origin</code> of the <code>bounds</code> for the <code>view</code>. It's divided by <code>20</code> because the tile size is <code>20,</code> so when it's at <code>(1,2)</code>, it's actually at <code>(20,40)</code>. The only reason I do this is to make it easier to manipulate (It's easier to count in units of 1 as opposed to 20). The four <code>for</code>'s go through and check that the <code>[player_ location]</code> is within <code>15</code> tiles of the center (<code>bounds + 15</code>) of the <code>view</code>. If the player is moving towards one of the edges of the screen, and <code>bounds.x/y + 30</code> is less than the height of the current map/width, it moves the <code>origin</code> so that the player is still centered and displayed on the map. </p> <p>The code works perfect, and I moved the <code>setbounds</code> to after the <code>for</code>'s happen, and there's only one. It isn't being left in <code>drawRect</code>, I just had it here to try and figure out what I needed to do. It's now in it's own place and is only called when the player actually moves. </p> <p>Here's the new code:</p> <pre><code>- (void)keepViewCentered { NSPoint pl = [player_ location]; NSPoint ll; NSPoint location = [self bounds].origin; ll.x = location.x / 20; ll.y = location.y / 20; for ( pl.y = [player_ location].y; pl.y &gt;= ll.y + 15 &amp;&amp; ll.y + 30 &lt; [currentMap_ height]; ll.y++ ) { location.y = ll.y * 20; } for ( pl.x = [player_ location].x; pl.x &gt;= ll.x + 15 &amp;&amp; ll.x + 30 &lt; [currentMap_ width]; ll.x++ ) { location.x = ll.x * 20; } for ( pl.y = [player_ location].y; pl.y &lt;= ll.y + 15 &amp;&amp; ll.y &gt;= 0; ll.y-- ) { location.y = ll.y * 20; } for ( pl.x = [player_ location].x; pl.x &lt;= ll.x + 15 &amp;&amp; ll.x &gt;= 0; ll.x-- ) { location.x = ll.x * 20; } [self setBoundsOrigin: location]; } </code></pre> <p>Here are pictures of it in action!</p> <p>Figure 1: This is the player at 1,1. Nothing special. <a href="http://sneakyness.com/stackoverflow/SRLbounds1.png" rel="nofollow noreferrer">http://sneakyness.com/stackoverflow/SRLbounds1.png</a></p> <p>Figure 2: The 3 gold pieces represent how far the player can move before the view's bounds.origin will move to stay centered on the player. Although irrelevant, notice that the player cannot actually see the gold. Turns out programming field of vision is a field of much debate and there are several different algorithms you can use, none of which don't have downsides, or have been ported to Objective-C. Currently it's just a square. See through walls and everything. <a href="http://sneakyness.com/stackoverflow/SRLbounds2.png" rel="nofollow noreferrer">http://sneakyness.com/stackoverflow/SRLbounds2.png</a></p> <p>Figure 3: The view with a different bounds.origin, centered on the player. <a href="http://sneakyness.com/stackoverflow/SRLbounds3.png" rel="nofollow noreferrer">http://sneakyness.com/stackoverflow/SRLbounds3.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload