Hunt soldiers GAME

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
DonJohnnie
Posts: 18
Joined: February 8th, 2017, 2:18 pm

Hunt soldiers GAME

Post by DonJohnnie » February 26th, 2017, 8:20 pm

I've been coding up some stuff with all the knowledge Chili has provided us with and also using some concepts which have not been entirely covered yet.

The game is very simple; it uses concepts across the Poo game, Fart Annoyed and so on.

I'm thinking about taking some advice from some people considering some new features I could add to the game, provided they're not super hard to implement and also maybe some ideas for making the code look/work better.


Here's the git:
https://github.com/donjohnnie/DON-Soldier-Game

CONTROLS :
ARROW KEYS TO MOVE,
NUMPAD 8, 4, 5, 6 to shoot in a specific direction.
I suggest holding on the key because for some reason if you move the soldier and try to tap a shoot button, it doesn't work as it's supposed to. However, if you move the soldier and hold a shoot button or more, it works just fine.


As you can probably see, the evade() system is quite primitive. I was thinking about making it work like a 1 chance out of 2 to dodge a bullet or something like that (50% or more). There wasn't much time put into the accuracy of the evade, so the enemy can still be hit even if it is redirected by the function. I kinda just put it there to see how it works and I didn't want to make Steven Seagal-type enemies. (saying that I realise that the soldier can shoot in 4 directions at the same time; uhhhhhhhhhhhhhh)

The start/game-over/resart will be added eventually.

Looking forward to your opinions :)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hunt soldiers GAME

Post by chili » February 27th, 2017, 1:41 am

Dude, I haven't had a chance yet to clone and run this, but I just looked at your code, and it looks pretty damn solid. It's super easy to follow what's going on. Also, nice work discovering std::vector<> for yourself. Makes life easier doesn't it? ;)

I will get back here once I've tried the game out. Thanks for sharing :)
Chili

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

Re: Hunt soldiers GAME

Post by Pindrought » February 27th, 2017, 1:51 am

Sick.

Pro tip to anyone trying it out: turn on num lock.

I looked through your code thinking the shoot function wasn't in until I realized I didn't have numlock turned on. :(
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

User avatar
DonJohnnie
Posts: 18
Joined: February 8th, 2017, 2:18 pm

Re: Hunt soldiers GAME

Post by DonJohnnie » February 27th, 2017, 2:59 pm

Thanks!! :)

About vector - yeah, it definitely makes things feel easier. The built-in array seems very bulky to me and overall harder to manage.

Also did the following changes:
-deleted some leftover variables from earlier on in the project;
-modified the evade function a bit; now it works better and the enemies dodge better;
-if you hit their bottom while they're going upwards or their head while going down(enemy.y < 0 && bullet.y < 0 and opposite situation), they can't dodge
-if the enemy was near a wall, close to getting a deflection AND was in the dodge range of a bullet (which would also be near the wall), it would get stuck between the two [vibrating] -> solution? - now a bullet can't be dodged twice

There appears to be a small problem with the constructor I think, I changed both the xDist and yDist to (50,500) to avoid drawing outside the screen. The error occured quite rarely (exactly when starting the game; maybe like 1 time out of 20 I've tried and only in the debugger) so I can't be sure if that's the solution, but I guess it would be something that includes random numbers, and the xDist and yDist were my first catch


OVERALL, the game should feel a lot nicer now, especially the shooting and dodging part

User avatar
DonJohnnie
Posts: 18
Joined: February 8th, 2017, 2:18 pm

Re: Hunt soldiers GAME

Post by DonJohnnie » February 27th, 2017, 6:35 pm

1.Cleaned the code up a bit - sorted the member functions by role, ordered the private members and got rid of some long in-class functions definitions like the checkAttack(). Now the code in the header files is easier to follow.

2.Also made the soldier able to shoot only in one direction, so no more Rambo shit like I first wanted. Considering the default number of enemies and the speed of the characters, the difficulty is definitely higher now and you gotta put some effort into dodging the enemies' bullets and to figure out how to trap them using yours.

For the moment I'll be waiting for more opinions and maybe some people (maybe Chili because he seems to like giving challenges) could give me some ides for improvement and stuff to add to the game.

Personally I was thinking about something along these lines:
-simple cover system;
-power-up that enables Rambo Mode (shoot in 4 directions at the same time);
-power-up that makes you go faster, etc;
-a bomb on the screen and if you hit it it's game over;
-something that if you hit it spawns one more enemy;
-health and health bar;
-maybe different difficulties (in the evade function I would put some random numbers and use them for a better/worse chance of enemies dodging)

User avatar
Theo
Posts: 2
Joined: February 26th, 2017, 6:37 am

Re: Hunt soldiers GAME

Post by Theo » February 28th, 2017, 11:50 am

Hey dude how did you make the enemies aim at the dude, and then be able to doge the bullets?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hunt soldiers GAME

Post by chili » February 28th, 2017, 1:28 pm

Looking good man. Nice work on the evasion. Like I said, code organization is good.

My challenge: make it so that you can aim in any direction using the mouse, and make it so that single clicks fire properly (instead of having to hold it down before it starts firing). Are you man enough? :P
Chili

User avatar
DonJohnnie
Posts: 18
Joined: February 8th, 2017, 2:18 pm

Re: Hunt soldiers GAME

Post by DonJohnnie » February 28th, 2017, 3:34 pm

Theo wrote:Hey dude how did you make the enemies aim at the dude, and then be able to doge the bullets?
Well let's say we have 1 enemy. The spawn location of each bullet is the enemy's center. Now you want the bullet to move from the center of the enemy to the center of the dude (Soldier more specifically), meaning you actually need a vector going from the enemy's center to the soldier's center. Let's say that we have u vector for soldier's location and v vector for enemy's location. The vector you're looking for is u - v.

This is the code:

Code: Select all

(s.GetCenter() - GetCenter()).GetNormalized()*1.0f

//s.GetCenter() gets the center of the Soldier-type object s
//you substract the two vectors and then you normalize them, because you don't want that big of a value, and then multiply them with a number, considering the speed you want the bullets to move at
Here's the drawing. The circle is the soldier and the square is the enemy (these figures have noting to do with the actual way you treat them code-wise, they're just for the sake of the representation):

http://i.imgur.com/3GRjEZV.jpg

As for the enemies being able to dodge the bullets, first you get the the vector from a bullet's center (fired by the soldier) to the center of an enemy (or vice-versa, it doesn't matter since the length is the same for both cases ). Then you get the length of this vector and compare it with a maximum "dodge range" you want :

Code: Select all

 (el.getPos() - pos).GetLengthSq() <= 65.0f*65.0f
If it's equal to or anything lower than that, you proceed to negate the x and y values of the enemy, which does the same thing as hitting a wall, thus "evading" the bullet.


@Chili

Thanks for that!
I'll definitely try to do both! It doesn't sound that hard... Gonna make some free time for it as there are some exams coming :D


Apparently something was wrong with checkAttack() and the actual variable was being constantly reset to 0 if the key was not held (or some other odd shit, i dunno). I changed things up a bit; now the code looks uglier but it's working correctly - you don't have to hold the key anymore
Last edited by DonJohnnie on February 28th, 2017, 7:51 pm, edited 1 time in total.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hunt soldiers GAME

Post by albinopapa » February 28th, 2017, 5:25 pm

Let's say that we have u vector for soldier's location and v vector for enemy's location. The vector you're looking for is i - v.
This is why I have a hard time learning maths on the internet. You setup variable names (u and v) then in the forumula you us use 'i' and 'v'.

This could have been a typo sure, but this is a common thing I've come across when someone tries to explain how things work when it comes to math, especially algebra where you deal less with concrete numbers and more in abstract letters and symbols. The hardest part is having to learn the language before being able to understand the math. So when people go through the trouble of setting up scenarios or examples with one set of variable names then use different variable names in the formulas, you'are stuck wondering "WTF! Where did these variables come from? They weren't mentioned in the instructions or previous paragraph."

DonJonnie is right though, if you want an object to travel in a direction toward another object, you simply subtract the origin position from the target position to get a vector that points in the targets direction. Next you normalize the vector, which give you a vector with a length of one (called a unit vector ). Now that it is normalized to a length of one, you can scale the vector based on the projectile's speed just by multiplying the unit vector by the projectiles speed.

Here's a little cheat sheet:
Vec2 origin;
Vec2 target;

// Subtract origin from target
Vec2 deltaVector = target - origin;

// Normalize the deltaVector to create a unit vector
// To normalize a vector, you use the Pythagorean theorem
// C^2 = A^2 + B^2 => (C = sqrt(A^2 + B^2);

// First, square each of the components of the vector, then add the results together.
float squareLength = (deltaVector.x * deltaVector.x) + (deltaVector.y * deltaVector.y);

// Now to finish getting the length, just take the square root of the squareLength
float length = sqrt(squareLength);

// Now to normalize the vector, you just need to divide the components of the vector by the length
// The operator/ can be overloaded to do this step, but I wanted to show the steps
Vec2 directionToTarget = Vec2(deltaVector.x / length, deltaVector.y / length);

// Something that didn't occur to me for a long while, is when you normalize the vector, you end up with x = cosine, y = sine of the angle the vector makes.

// Now you can multiply this vector by some scalar ( a single value ) to get any point on the ray going form Origin in direction of the Target.


Hopefully, I didn't make things more confusing.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
DonJohnnie
Posts: 18
Joined: February 8th, 2017, 2:18 pm

Re: Hunt soldiers GAME

Post by DonJohnnie » February 28th, 2017, 7:56 pm

Lol, sorry for the typo mate, but even with the vector knowledge Chili taught, there shouldn't have been a problem understanding that I meant u instead of i (plus I also showed the figure)...


Also, finished Chili's challenge. Now you can easily shoot with the mouse in any direction you want. I appreciate everyone's activity on this thread and keep the feedback coming pls :D. I want to improve and add to the game as much as I can.

Post Reply