Awesome tip!
By 'freeware' do you mean something like the Unlicense? http://unlicense.org/
I have to be a bit of a stickler about this, because I'm modelling proper digital citizenship for my kids. :)
Also added
if ( dsx == 0 && dsy == 0 ) return;
to the beginning of scroll(int dsx, int dsy) so that no work is done if the requested scroll is zero.
I love this code. Most compact way of implementing this that I've seen. Very well done!
What license is this code under? I have some students interested in using it!
Yes indeed. The change I posted makes the value positive before the mod. They always land on the same value.
So instead of:
------------------------------
// find a similar positive value for scroll positions
while (imageX < 0) imageX += 100*wide;
while (imageY < 0) imageY += 100*high;
// get new starting positions for drawing 'scrollImage'
imageX = imageX%wide;
imageY = imageY%high;
------------------------------
You can have:
------------------------------
// find a similar positive value for scroll positions
if (imageX < 0) { imageX %= wide; imageX += wide; }
if (imageY < 0) { imageY %= high; imageY += high; }
// get new starting positions for drawing 'scrollImage'
imageX = imageX%wide;
imageY = imageY%high;
------------------------------
Or... given that it looks like you may have added the multiplication by 100 to reduce the number of loops....
if (imageX < 0) { imageX %= wide; imageX += wide; }
if (imageY < 0) { imageY %= high; imageY += high; }
Should get you there without the loop and without the x100 workaround to try and reduce looping.
In the scroll() method, the following two lines with loops may take a long time to execute if the 'camera' is scrolled very far from world center:
while (imageX < 0) imageX += 100*wide;
while (imageY < 0) imageY += 100*high;
--- Consider updating this to
if (imageX < 0) { imageX %= 100*wide; imageX += 100*wide; }
if (imageY < 0) { imageY %= 100*high; imageY += 100*high; }
--------------------
This replaces a looped add with a single mod and a single add.
@AreebRaza:
There are some loops in the createTextur() method of the ScrollingWorld class that might take a long time to complete when the player is very far afield. Here's a version without the loops that uses mod math to do the same job.
/**
* Creates a moving textur on the background image of the world.
*/
protected final void createTextur() {
// Higher performance version:
int x = totalXMovement;
x %= textur.getWidth();
if ( x > 0 ) x -= textur.getWidth();
int y = totalYMovement;
y %= textur.getHeight();
if ( y > 0 ) y -= textur.getHeight();
getBackground().clear();
for (int i = x; i < getWidth(); i += textur.getWidth()) {
for (int j = y; j < getHeight(); j += textur.getHeight()) {
getBackground().drawImage(textur, i, j);
}
}
}
I like it! It'd be more difficult if the player could die even when moving.
To keep that from making it too difficult, you'd want to add the ability to face/move all 8 directions, and have the World only add new zombies at a certain minimum distance from the player.
This is some really great work!
I've been working on extending this as a base for a real-time strategy game a student of mine wants to create. I seem to be having problem with collision detections utilizing getObjectsAt(int x, int y). It seems like that method has some weirdness when called off screen.
2017/2/21
Scrolling Tutorial
2017/2/20
Scrolling Tutorial
2017/2/20
Scrolling Tutorial
2017/2/17
Scrolling Tutorial
2017/2/17
Scrolling Tutorial
2017/2/17
Infinite Scrolling World
2013/4/19
Zombie Horde
2013/3/28
Mancala
2012/5/1
ScrollWorld