I am working on an arcade style game in Java, and using it, initially, to build a flocking/stalking simulator for a zombie game. The code will ultimately be translated to C# and plugged into Unity. But I am far more versed in Java so I want to build it there and avoid the Unity learning curve. When we go to implement it, I'll work with the Unity developer.
I am working on player movement, because the same movement class will drive the enemies. I ran into the same problem I had with The Swarm - my previous arcade game. When a direction key is pressed, then another direction is pressed, the key up events for these wreak havoc and you end up with the player sitting still. I came up with an amazing and SO simple solution for this and the results are everything I hoped for.
The movement class has:
- speedX / speedY
- x / y
- speedX / speedY
- x / y
Then I added:
- moveX / moveY
- moveX / moveY
The last two work with the key presses. (Since it's Java up is negative, down is positive) If the player hits the left direction key, moveX is assigned -1, if the right direction key is pressed moveX is assigned 1. Then the moveX and moveY values are multiplied by the speed and added to the x and y.
In the key release, if the left key is released and the moveX is negative, I set moveX to 0, if the right key is released and the moveX is positive I set moveX to 0.
In the key release, if the left key is released and the moveX is negative, I set moveX to 0, if the right key is released and the moveX is positive I set moveX to 0.
What's great about this is I can easily set movements in eight directions. When we go to implement it, of course it will need to be expanded and improved for a much more complex game but it will allow us to work out the routing and flocking algorithms.
No comments:
Post a Comment