can you have some fighting in your game?

Started by shaungaryevans, Tue 09/03/2010 22:37:02

Previous topic - Next topic

shaungaryevans

Hi,

I have been using AGS for about 1 years now and i mastered all the simple techquies (hotspots, rooms, varibles etc). Now I would like to do a bit of action in my game, maybe a bit of fighting when you have to beat emeries. I have got no idea of how to start this kind of scripting off, can somebody advise me a simple script to learn to start please?

Thanks in advance

monkey0506

Code: ags
if (player.IsCollidingWithChar(cEnemy)) {
  cEnemy.Say("I AM DEAD!");
  cEnemy.ChangeRoom(-1);
}


That's a pretty simple one, I know, but it works..sort of..depending where you put it..maybe. :D

Crimson Wizard

#2
Seriously, monkey is right in his way, there's no simple script for that kind of things. It usually requires lots of scripting, but before scripting it requires lots of thinking, planning module logic, etc.

By the way, there's Ahmed's fighting game available, maybe you will find it useful, it has open code: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=20327.msg515108#msg515108

Khris

shaungaryevans:

If you just want some working fighting code, try to be more specific (but don't expect a full-fledged fight engine script). Then we can give you some pointers.

If you rather want to add other advanced stuff later on and be able to do that yourself, you're going to need to learn how to tackle a scripting problem. I'm not sure where to point you; the web is full of tutorials but those are almost always aimed at learning a specific language.
In general, break down the engine into smaller parts, start simple and solve one problem at a time.

A good starting point would be implementing a numpad controlled sprite that can move and punch.

monkey0506

It also depends on your fighting style that you want to achieve. Presumably you are talking about some sort of real-time fighting, but if you want turned-based it will be totally different. Also to take into consideration, will the fighting be projectile or melee?

There's a lot more to "adding fighting to your game" than simply adding a couple lines of script. The snippet I posted could, after a fashion, prove useful depending on how your fighting will be managed. However, there is no catch-all generic (or simple for that matter) solution.

Mr Flibble

Something I've been wondering about for a long time, which has applications here, is the idea of being able to split up sprites into regions or areas. It'd be useful for, for instance, fighting game collision detection (ie. did the enemy collide with a fist or just the body* or where did a specific punch land), or switching out segments of a character (like how the old LEC games worked, they didn't store the whole sprites each time, just the segment that was changing) or for having characters only being partially tinted by a region (for instance, so you could have them standing half in shadow, half in light)


*A nice and simple way to achieve this with existing behaviour would be to check if the sprites are colliding, then also check if the current frame of the player's (or enemy's) view. This would mean that the player would have to "punch" to cause damage, not just collide by walking into the enemy.
Ah! There is no emoticon for what I'm feeling!

CShelton

Quote from: Mr Flibble on Wed 10/03/2010 02:19:26
Something I've been wondering about for a long time, which has applications here, is the idea of being able to split up sprites into regions or areas. It'd be useful for, for instance, fighting game collision detection (ie. did the enemy collide with a fist or just the body* or where did a specific punch land)

With some hackery and SSH's Pixel Perfect Collision Module you could build a complex character that has fist and foot collision areas. This would be accomplished by having multiple characters occupying the same space, each limb being a separate character with their own collision. Orchestrating all of this is no easy feat, though it can be done.

Khris

Or you could use structs to store the coordinates of collision boxes for every frame. Similarly annoying to set up but faster and easier to change.

shaungaryevans

Hi Guys,

Thanks for all the pointers, so i need to start from the begining on this a work on it, first thing i would suppose is the enemy's movement. He will walk on this own around the map, sorry to be a pain but how can you make a character move around the map/room without you controlling it?

monkey0506

Well it depends whether you want them to walk along a predefined path or just wander around aimlessly at random. The first would involve creating some way of defining the path, the points along it to which the character will travel, and then the order in which each point is reached. That would probably be done using some sort of struct.

The second is much simpler:

Code: ags
function repeatedly_execute_always() {
  if (!cEnemy.Moving) cEnemy.Walk(Random(Room.Width), Random(Room.Height));
}

suicidal pencil

Quote from: monkey_05_06 on Thu 11/03/2010 17:27:31
Code: ags
function repeatedly_execute_always() {
  if (!cEnemy.Moving) cEnemy.Walk(Random(Room.Width), Random(Room.Height));
}


what about walkable areas and the such? If you had an urban scene, I think it would be a bit weird to watch an enemy walk up the side of a building :P


monkey0506

The Walk function can take account of walkable areas. In the example provided you'd presumably want to use eWalkableAreas and eNoBlock. Don't remember what the defaults are, but that's what the manual is for.

shaungaryevans

Thanks, i have now got someone that moves on his own, but one problem, the timer cursor stays on while he moves. I don't remember how to make the timer go off? Any help please?

monkey0506

Okay just checked in with the manual and the default settings are eNoBlock and eWalkable areas so unless you specifically pass something different it shouldn't be blocking at all.

What is the exact code you are using?

shaungaryevans

if(cEgo.Room == 1) cChar1.Walk(Random(Room.Width), Random(Room.Height), eNoBlock);

monkey0506

You forgot to check that the character isn't moving:

Code: ags
if ((cEgo.Room == 1) && (!cChar1.Moving)) cChar1.Walk(Random(Room.Width), Random(Room.Height));


eNoBlock is the default so you don't need to pass that in explictly.

shaungaryevans

Thanks, that works now.

Back to the getting to the frighting bit now, now i know how to move cchar1 freely, can i make him to come towards me all the time as what enemys do please?

monkey0506

Do you want him to chase after you if you get within a certain distance, if he can see you, or just all the time?

Regardless of when you want him to chase you, look up the Character.FollowCharacter function in the manual.

shaungaryevans

#18
I had a look at the manual now, its only got how to set the distance between charatcher the the standing time. How to set set as if they look at him or he get anywhere near him, they chase him

monkey0506

You will have to script that yourself as it's not a built-in method. Something like:

Code: ags
int DistanceFromCharacter(this Character*, Character *theCharacter) {
  // based on a script by KhrisMUC
  if (theCharacter == null) return -1;
  int z1 = 10000 / this.Scaling;
  int z2 = 10000 / theCharacter.Scaling;
  int x1 = this.x * (z1 / 100);
  int x2 = theCharacter.x * (z2 / 100);
  int dx = x1 - x2;
  int dz = z1 - z2;
  return FloatToInt(Maths.Sqrt(IntToFloat((dx * dx) + (dz * dz))));
}

bool IsFacingCharacter(this Character*, Character *theCharacter) {
  if (theCharacter == null) return false;
  bool turnBeforeWalk = GetGameOption(OPT_TURNBEFOREWALK); // whether characters turn before walking
  if (turnBeforeWalk) SetGameOption(OPT_TURNBEFOREWALK, 0); // temporarily disable this setting
  int loop = theCharacter.Loop; // store the current loop
  theCharacter.FaceCharacter(player, eNoBlock); // sets the character's loop towards the player
  bool facing = (theCharacter.Loop == loop); // whether theCharacter was already facing the this Character
  if (!facing) theCharacter.Loop = loop; // restore the previous loop if changed
  if (turnBeforeWalk) SetGameOption(OPT_TURNBEFOREWALK, 1); // restore turn before walking if disabled
  return facing; // return whether the player was facing the other character already
}

function repeatedly_execute() {
  if (player.DistanceFromCharacter(cChar1) <= 30) { // the player is within 30 units (scaled pixels) from the enemy's position
    if (cChar1.IsFacingCharacter(player)) cChar1.FollowCharacter(player, 1, 10); // or whatever values you want here
  }
}

SMF spam blocked by CleanTalk