Most basic combat system

Started by SebastianEl, Sat 11/05/2024 10:10:01

Previous topic - Next topic

SebastianEl

Hello, I'm looking for totally easiest to implement combat system for a beginner AGS user like me. I haven't seen any tutorials on this matter, and seen just some veeery old posts

Example sytem breakdown:

Two HP meters (opponent & player)

1. Use knife on opponent, walk to opponent, starting player turn
2. random player chance to hit (0-10),
3. apply random damage to creature (1-10),
4. automatic opponent turn,
5. random opponent chance to hit
6. apply random damage to opponent
After change to hp 0 player/opponent sprite to "dead"


Is it doable, and how difficult would be to implement it? :)

Also: are there maybe any code fragments free to use that could facilitate the process for the begginer? :)

Thank you in advence!





Khris

#1
It is definitely doable.

A health bar for instance can be implemented using a button on a GUI. Use the GUI's outline or background color as border, put a non-clickable button on it with a suitable image (just a green sprite for instance) and resize it according to the health points i.e. set the button's .Width to a new value.

Randomizing stuff is very simple:
Code: ags
  int result = Random(10); // 0 - 10
  result = Random(9) + 1; // 1 - 10

You also need variables to store each character's HP. At the top of the global script, add lines for each variable you need:
Code: ags
int playerHP = 100;
int enemyHP = 50;

Next I'd use a battle function:
Code: ags
function Battle(Character* enemy, int initialHP) {

  enemyHP = initialHP; // set enemyHP

  int damage; // local variable

  // keep the fight going until either character dies
  while (playerHP > 0 && enemyHP > 0) {
    // player turn
    // player's attack animation goes here
    if (Random(99) < 30) { // 30% chance
      damage = Random(9) + 1;
      enemyHP -= damage; // enemy loses HP
      // enemy gets hit animation here
      if (enemyHP < 0) enemyHP = 0; // no negative values
      btnEnemyHP.Width = enemyHP * 2; // update enemy health bar
      Wait(1); // ensure the screen updates, can possibly be skipped
      if (enemyHP == 0) Display("You stab %s for %d damage, killing them.", enemy.Name, damage);
      else Display("You stab %s for %d damage.", enemy.Name, damage);
    }
    else Display("You stab at %s but miss.", enemy.Name);
    if (enemyHP == 0) break; // leave while loop if enemy is dead
    // enemy turn goes here
  }
}

Now you can do the character interaction as usual:
Code: ags
function cGoblin_Interact(...) {
  // make player approach enemy here
  Battle(cGoblin, 50); // battle the goblin, give them 50 HP
}

The important thing here is that you don't just blindly copy this to your script and expect it to work; you need to look at the code, understand what each line does and study the explanation of the used commands in the manual.
You can also ask here, but learning how to program takes a while, and isn't really what this forum is for.

Crimson Wizard

Some years ago I have posted an advice regarding development of a combat system (this advice may be used for any system).
Now I am reposting this every time this question comes up.

Original thread:
https://www.adventuregamestudio.co.uk/forums/beginners-technical-questions/coding-fight-ai/msg636578340/#msg636578340

QuoteCoding is not only about knowing the programming language, but first of all about writing algorithms and structuring your program.
Complex algorithms are usually broken into smaller parts, to make it easier to understand and code.

My biggest advice to anyone who begins to learn programming and wants to make something complex, like fighting in this topic:
First, write down what you'd like to have "on paper". At this stage there's no need to go into much details, just rough outline. What objects would you like to have on screen, how these objects act, how player is supposed to act.
Second, try to break the whole scene down to smaller tasks, and learn to make these one by one, separately. After you did, you will be able to combine complex scene out of those pieces.

For example, you want to have QFG-like combat. This involves a picture of a player that swings sword (or does other stuff) by player's command and picture of enemy that does actions by AI command. Essentially this means that there is some visual character that receives commands from a source. Source can be either player or AI script.

Try following: make a simplier scene (perhaps in a test game), where you have a character on screen, that runs animations when player presses a button. Learn how to test when action ends, so that player could not run new actions before that.

Then make another scene where same character is controlled by a script. Learn how to make it launch same actions randomly, by a timer.
It may be important to learn how to "detach" animation script from AI script, to make them independent of each other. This way you will be able to connect same AI script to multiple characters.

Next step: teach AI to react on opponent's actions. You do not have to have actual opponent for that. Implement "imaginary" opponent. For example, remember what opponent is doing (defending, attacking, etc) in a variable, put a text on screen telling that to make it easier for you, and let player change that imaginary action by key press. Make AI react to this variable: when you set it to "attack" - AI should defend, and vice versa (or other variants).

Fourth scene could be where two characters stand face to face. They may be even both controlled by AI. Make them fight with each other using techniques you learnt in previous three scenes.

After you succeed in this, move on to combat rules and GUI part, with health, winning and loosing conditions, and so forth.

SebastianEl

OMG @Khris THANK YOU!!

I'm super excited right now, although I know it takes me a loong time to grasp it and implement it in working condiotion :D

SebastianEl

I realized that "most basic" system is not as basic as it could be :D

It could be far simpler just for to start:

Player: uses gun from inventory and uses on NPC on screen. (On screen gun from inventory changes to crosshair?).
Game: chacks if he has a [bullet] in inventory, if true:
SFX of a gunshot is played
NPC changes sprite to "dead NPC"
Bullet is removed from inventory

Am I right that this would far easier and possible to implement for a begginer?

SMF spam blocked by CleanTalk