Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Mouse directed attack with 4 possible outcomes and animation based on attack.  (Read 2420 times)
0 Members and 1 Guest are viewing this topic.
Offline ZeyMeijin

Senior Newbie

Medals: 1

« Posted 2017-12-21 09:46:54 »

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:

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

      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? Smiley
Btw. This is my first post here, hi everyone! Im learning java since dec 4th. xD

Online VaTTeRGeR
« Reply #1 - Posted 2017-12-21 12:52:08 »

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

You don't really need the angle though Grin

Can you please tell me how to get this code better

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.

face me in the right direction

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:

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...
Offline ZeyMeijin

Senior Newbie

Medals: 1

« Reply #2 - Posted 2017-12-21 16:19:03 »

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! Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ZeyMeijin

Senior Newbie

Medals: 1

« Reply #3 - Posted 2017-12-21 17:27:00 »

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.
Online VaTTeRGeR
« Reply #4 - Posted 2017-12-21 20:08:03 »

Oh, now i get what you mean...

tried creating if for each direction but errors

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) {
} else if(45 < angle && angle < 135){
} else if(-135 < angle && angle < -45){
} else if(135 < angle || angle < -135){
Offline ZeyMeijin

Senior Newbie

Medals: 1

« Reply #5 - Posted 2017-12-21 20:22:58 »

Thank you, my problem was that i failed at if, i just didnt try to use
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. Smiley Its good to get out of stuckhole.
Offline philfrei
« Reply #6 - Posted 2017-12-21 20:37:11 »

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();

music and music apps:
Pages: [1]
  ignore  |  Print  

Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4585 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4016 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!