• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Lakhai

Damaging Enemy AI

4 posts in this topic

Hello everyone!

This is my first topic, since I've started reading these forums a while ago.
So I finally felt ready to start a little "game" project in C++; I've been using this language on and off for a bit, and I thought a project would help me understand it and practice it more.

What I want to do is a text-based RPG (focusing more on combat system, like Pokémon for example) and for now I'm just trying to get the basics to work.
You can make a character, name him and the game will randomly generate an amount of HP for you.

So I made this for the enemy:
[CODE]
class Enemy
{
public:
int health;
int curhealth, atk1;
string name;
int GetHealth();
void SetHealth();
int TakeDamage(int);
};
[/CODE]

Everything is going fine, but when I tried damaging one of those enemies with this function:
[SPOILER]
[CODE]
void Player::Attack(Enemy enemy)
{
int atk = urand(10, 30);
enemy.TakeDamage(atk);
cout << endl << "Attacked for: " << atk << "." << endl;
cout << "Enemy HP: " << enemy.GetHealth() << "." << endl;
}
// Enemy
void Enemy::SetHealth()
{
health = urand(100, 120);
curhealth = health;
}
int Enemy::GetHealth()
{
return curhealth;
}
int Enemy::TakeDamage(int damage)
{
curhealth -= damage;
}
[/CODE]
[/SPOILER]

The first time I damage the enemy the health returns fine( ex. Enemy health is 100, I attack for 20 it returns 80);
now, the second time instead of taking the current health value of 80 and reducing, say another attack of 10, it'll reduce to the total health, returning 90.

I think that's because it's always re-setting curhealth to the value of maxhealth, therefore everytime I damage him it ignores the times before.

This is probably a silly mistake, and I'll try to fix it tonight if I have time, but a little insight wouldn't hurt [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img].

Apart from that, if you have any ideas as to what I could put in the game, let me know!
Cheers [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

EDIT: It now works just fine, thanks to Servant of the Lord and Sollum! Edited by Lakhai
0

Share this post


Link to post
Share on other sites
I think you're not showing entire code. Took the methods you provided and put mechanics into my code, works fine.
By the way, just a little tip. Avoid stuff like

useSuperMegaAwesomeFunction(4, 6, 1, "crab", 54, 2, 4, 2, 'c', JUST_RANDOM.ENUM, "GO!");

It will confuse you later on. Don't be afraid to write few additional Int's as MIN_DAMAGE = 10 and MAX_DAMAGE = 30

I add working code, tho it's in C#, but logic behind in the same.

[source lang="csharp"]using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Enemy Goblin = new Enemy("Goblin", 1);
Enemy Link = new Enemy("Link", 15);

Link.Attack(Goblin);
Link.Attack(Goblin);
Link.Attack(Goblin);
Link.Attack(Goblin);
Link.Attack(Goblin);

Console.ReadLine();
}
}

class Enemy
{
int health;
int curhealth, atk1;
string name;

public Enemy(string name, int atk1)
{
health = 100;
curhealth = health;
this.name = name;
this.atk1 = atk1;
}

int GetHealth()
{
return curhealth;
}

void SetHealth()
{
//null
}

void TakeDamage(int damage)
{
curhealth -= damage;
}

public void Attack(Enemy enemy)
{
enemy.TakeDamage(atk1);
Console.WriteLine(name + " attacked for: " + atk1 + ".");
Console.WriteLine("Enemy HP: " + enemy.GetHealth() + ".");
Console.WriteLine("====================================");
}
}
}
[/source] Edited by Sollum
0

Share this post


Link to post
Share on other sites
Well, here's my entire code:
Main.cpp
[SPOILER]
[CODE]
#include <iostream>
#include "Player.cpp"
#include "Enemy.cpp"
using namespace std;
int main()
{
int opc;
Player* player;
Enemy enemy;
player->SetHealth();
cout << "What's your name?" << endl;
player->SetName();
cout << endl << "Your name is: " << player->name << " and your health is: " << player->GetHealth() << endl;
// First enemy
enemy.SetHealth();
enemy.curhealth=100;
cout << endl << "An enemy appears!" << endl;
cout << "Enemy name: Chobi." << endl << "Enemy HP: " << enemy.GetHealth() << "." << endl;
option:
cout << endl << "What to do?" << endl;
cout << "1. Attack #1." << endl;
cin >> opc;
if (opc==1)
player->Attack(enemy);
goto option;
return 0;
}
[/CODE]
[/SPOILER]

Player:
[SPOILER]
[CODE]
// Player.h
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <cstring>
#include "Enemy.h"
using namespace std;
class Player
{
public:
int health, curhealth, atk1, atk2;
string name;
int GetHealth();
void SetHealth();
void TakeDamage(int);
void SetName();
void Attack(Enemy);
};
#endif // PLAYER_H_INCLUDED
// Player.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Player.h"
using namespace std;
int urand(int min, int max)
{
srand(time(NULL));
return rand() % (max - min + 1) + min;
}
void Player::SetHealth()
{
health = urand(100, 200);
curhealth = health;
}
int Player::GetHealth()
{
return curhealth;
}
void Player::TakeDamage(int damage)
{
curhealth -= damage;
}
void Player::SetName()
{
getline (cin, name);
}
void Player::Attack(Enemy enemy)
{
int atk = urand(10, 30);
enemy.curhealth = enemy.TakeDamage(atk);
cout << endl << "Attacked for: " << atk << "." << endl;
cout << "Enemy HP: " << enemy.curhealth << "." << endl;
}
[/CODE]
[/SPOILER]

Enemy:
[SPOILER]
[CODE]
// Enemy.h
#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED
#include <cstring>
#include "Player.h"
using namespace std;
class Enemy
{
public:
int health;
int curhealth, atk1;
string name;
int GetHealth();
void SetHealth();
void TakeDamage(int);
};
#endif // ENEMY_H_INCLUDED
// Enemy.cpp
#include <iostream>
#include "Enemy.h"
using namespace std;
void Enemy::SetHealth()
{
health = urand(100, 120);
curhealth = health;
}
int Enemy::GetHealth()
{
return curhealth;
}
void Enemy::TakeDamage(int damage)
{
curhealth -= damage;
}
[/CODE]
[/SPOILER]
0

Share this post


Link to post
Share on other sites
This is unrelated to your problem (which I answered while you were posting your last post) and isn't a bug, but worth pointing out:
[code]class Enemy
{
public:
int health;
int curhealth, atk1;
string name;
int GetHealth();
void SetHealth();
int TakeDamage(int);
};[/code]

'SetHealth()' implies that the user of the class gets to choose what to set the health to. "ResetHealth()" would be a better name.
'heath' implies it's the current health. 'maxHealth' would be a better name.
'curhealth' is slightly confusing. 'curHealth' or 'cur_health' is better ([size=2]depending on your preference, but the first is more commonly used by C++ programmers, and the second is more commonly used by the C++ standard library because it was popular when the standard was being created[/size]), so you can see the beginning of each word. Even better still is 'currentHealth', I would rather have the extra readabillity, even if need to type four extra letters.

'TakeDamage(int)' should either return void, or return how much health is left. Currently you have it return int, but it doesn't actually return anything, meaning that the return value is gibberish.

[hr]

What's the point of a GetHealth() function if the actual 'curhealth' variable it returns is 'public'? 'curhealth' should probably be private. A class can access it's own private variables and functions, but outside code can't access them.

Something like this:
[code]class Enemy
{
public:
int GetHealth(); //Returns the monster's current health.
void SetHealth(int health); //Manually set the monster's current health to 'health'.
void ResetHealth(); //Resets the monster's health to maximum (maximum is a random amount between 100 and 120).
void TakeDamage(int amount); //Damages the enemy. Negative 'amount' values can heal the enemy, even beyond maximum.

private:
int maxHealth, currentHealth; //currentHealth and maxHealth are related, so it's okay to put them on the same line.
int attack1; //attack1 is only loosely related, so it should be on a different line. (This is my personal opinion!)
std::string name; //Use the 'std::' when in headers. Don't put "using namespace" in header files (This is not my personal opinion! It can cause problems. Ask if you want to know why)
};[/code] Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0