Mouse directed attack with 4 possible outcomes and animation based on attack.

Hi, i am trying to build simple hack n slash combat system(NOT A WHOLE GAME);
I want player to attack the direction in which mouse is pointed.
I managed to get simple code to achieve just the right angle:

          [b] [i]entyma[/i] = my entity manager //  [i]getX/Yos[/i] is getter for x/y offset // [i]Mb[/i] is mouseinput // [i]moy/x[/i] is mouse y/x [/b]



	int centerX = (int)(handler.getWorld().getEntyma().getPlayer().getX()-handler.getCamera().getXos());

	int centerY = (int)(handler.getWorld().getEntyma().getPlayer().getY()-handler.getCamera().getYos());

	double angle = Math.toDegrees(Math.atan2(centerY - handler.getMb().getMoy(), centerX - handler.getMb().getMox()));

I know its a bit messy, but im just starting with java, and i want to explore by practice, so yeah, i need it to play attack animation and generate rectangle and if that rectangle touches or overlaps with enemy/entity it deals dmg/prints to console that it hit(need just indicator that shows that it works).
Can you please tell me how to get this code better and preferably face me in the right direction? :slight_smile:
Thanks.
Btw. This is my first post here, hi everyone! Im learning java since dec 4th. xD

You calculated the angle from the player to the mouse position.

You don’t really need the angle though ;D

[quote]Can you please tell me how to get this code better
[/quote]
Get away from using all the get get get Methods in a chain multiple times, just get the object once and save it in a variable.

Instead of:

float v = handler.getWorld().getEntyma().getPlayer().getX() -
 handler.getWorld().getEntyma().getPlayer().getY() + handler.getWorld().getEntyma().getPlayer().getZ();

do this:

Player p = handler.getWorld().getEntyma().getPlayer();
[...]
float v = p.getX() - p.getY() + p.getZ();

Do not store the centerX/Y as an int, you need float (and sometimes double) anyway.

[quote]face me in the right direction
[/quote]
Read up on:

  • Vectors (and some other math stuff)
  • Collision between basic geometric forms like rectangle, point, circle, …

You should definitely check out this implementation of Rectangle and it’s collision/contains methods: Rectangle.java

Also check out the other (simpler) Classes in the package: gdx.math

Maybe someone here knows a good tutorial series to follow through, i don’t know a good one right now…

Thank you for your reply. Im not using libgdx yet, i want to get semigood with basic java or just confident enought to build the desired code fast enough.

I tough i need to generate angle to convert it to degrees to get -180 to 180 range(which i got eventually).

The problem is i really cannot implement it. If i put angle to if i get bunch of errors, it would be easier to just separate screen into 4 zones ranging from
-45 to 45, 45 to 135, [135 to 180 + (-180)to (-135)], (-135) to (-45), and then if lets say mouse is in the 2nd zone(lets say 75) it generates
the right attack animation when clicked(which is upwards).

Thanks for help! :slight_smile:

Sorry for doubleposting, THIS is what i mean, presented graphically:

These are rectangles, for example upslash is green part + half of upper red + half of upper pink.
Blue is player.
Also want player to stop moving when attacking, but yeah, thats just xmove=0 in attack method.

Managed to get angle, struggling with implementing it, tried creating if for each direction but errors.

Oh, now i get what you mean…

[quote]tried creating if for each direction but errors
[/quote]
Post some code, post some specific error messages, but this unspecific description does not tell me anything useful at all.

If you don’t know how to do the if else structure properly then you should read up on how to do that instead of asking here.

Is this what you need(?):

if(-45 < angle && angle < 45) {
   leftSlash();
} else if(45 < angle && angle < 135){
   upSlash();
} else if(-135 < angle && angle < -45){
   downSlash();
} else if(135 < angle || angle < -135){
   rightSlash();
}

Thank you, my problem was that i failed at if, i just didnt try to use
IF(X){
do();
}else
do2();
but i tried to set dir=x, well nice thing to get.
Now whats left for me is to call slashes Onclick depending on the angle, but it will be much easier and i will try to figure this out by myself.
Thank you so much again. :slight_smile: Its good to get out of stuckhole.

Slightly more compact form. Not sure if this makes intention clearer or not. For this sort of thing, performance differences are minute compared to the impact of code on readability, so coming up with easiest form for reading 6 months from now (i.e., “after you have forgotten what the code is doing”) should be the priority.

if (angle < -135) rightSlash()
else if (angle < -45) downSlash()
else if (angle < 45) leftSlash()
else if (angle < 135) upSlash()
else rightSlash();