Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
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 1418 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
Thanks.
Btw. This is my first post here, hi everyone! Im learning java since dec 4th. xD

Offline 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

Quote
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:
1  
2  
float v = handler.getWorld().getEntyma().getPlayer().getX() -
 handler.getWorld().getEntyma().getPlayer().getY() + handler.getWorld().getEntyma().getPlayer().getZ();


do this:
1  
2  
3  
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

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

Oh, now i get what you mean...

Quote
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(?):

1  
2  
3  
4  
5  
6  
7  
8  
9  
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();
}
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
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. 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: http://adonax.com
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

nelsongames (1173 views)
2018-04-24 18:15:36

nelsongames (1638 views)
2018-04-24 18:14:32

ivj94 (2395 views)
2018-03-24 14:47:39

ivj94 (1605 views)
2018-03-24 14:46:31

ivj94 (2691 views)
2018-03-24 14:43:53

Solater (882 views)
2018-03-17 05:04:08
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org 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‑gaming.org
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!