java - Is there a more concise way to move a game object over an oversize canvas? -
in teaching myself java , android development, may have relied on outdated methods (or no methods @ all!) think should common-place task: moving game object on oversize canvas. goal, of course, shift elements on canvas in opposite direction of object's movement when gets close edge (in direction)...until true edges of "world" encountered.
i use rectangles , polygons (i.e. sets of non-rectangular quadrilateral coordinates) test "hero" object on canvas (the "extended pond"). use negative delta (x,y) of object move everything, including object, keep object moving out of view. there methods @ our disposal more efficiently or concisely i've done below?
void shiftsurfaceviewtofollowgameobject() { //construct temporary rectangle pond surface matrix describes interpolated surface shift offset hero movement rect shiftedpondareaprojection = new rect(extendedpond.get(0).getx() - hero.dx, extendedpond.get(0).gety() - hero.dy, extendedpond.get(extendedpond.size() - 1).getx() + extendedpond.get(extendedpond.size() - 1).getwidth() - hero.dx, extendedpond.get(extendedpond.size() - 1).gety() + extendedpond.get(extendedpond.size() - 1).getheight() - hero.dy); int shiftunitsx = hero.dx; int shiftunitsy = hero.dy; //adjust shift amount horizontal , vertical constraints of borders if (!shiftedpondareaprojection.contains(viewarea.getrectangle())) { //calculate horizontal offset based on border restrictions if(shiftedpondareaprojection.left > viewarea.getrectangle().left) //shift far right { shiftunitsx = shiftedpondareaprojection.left + shiftunitsx - viewarea.getrectangle().left; } else if(shiftedpondareaprojection.right < viewarea.getrectangle().right) //shift far left { shiftunitsx = shiftedpondareaprojection.right + shiftunitsx - viewarea.getrectangle().right; } //calculate vertical offset based on border restrictions if(shiftedpondareaprojection.top > viewarea.getrectangle().top) //shift far down { shiftunitsy = shiftedpondareaprojection.top + shiftunitsy - viewarea.getrectangle().top; } else if(shiftedpondareaprojection.bottom < viewarea.getrectangle().bottom) //shift far { shiftunitsy = shiftedpondareaprojection.bottom + shiftunitsy - viewarea.getrectangle().bottom; } } //reciprocal move -- not actual "move" adjustment (delta x, delta y) prior drawing if (hero.getvisibility()) { //offset hero hero.setx(hero.getx() - shiftunitsx); hero.sety(hero.gety() - shiftunitsy); } //offset each tile section of extended pond (inanimateobject pondsection : extendedpond) { pondsection.setx(pondsection.getx() - shiftunitsx); pondsection.sety(pondsection.gety() - shiftunitsy); } //offset rock rock.setx(rock.getx() - shiftunitsx); rock.sety(rock.gety() - shiftunitsy); //offset each ripple in pond (ripple ripple : ripples) { ripple.setx(ripple.getx() - shiftunitsx); ripple.sety(ripple.gety() - shiftunitsy); } //...etc.
}
Comments
Post a Comment