How to check if the player is nearby something?

Started by Perpetuall, Sat 27/07/2024 01:14:08

Previous topic - Next topic

Perpetuall

I am revamping my combat system in my RPG. I am using the Pixel Perfect Collision module, and I currently have it rigged so that when an enemy touches the player, they receive damage. What I want is for the player to have to attack the enemy before they touch them. I have them equip a sword and use it on the enemy to attack, but I want it so that they have to get close enough before they can use it-but not close enough for them to touch the enemy, thus incurring damage. Just within range so they can swing the sword and hit the target. How do I script this? (P.S. Right now I'm using AGS 2.72)

Crimson Wizard

Quote from: Perpetuall on Sat 27/07/2024 01:14:08(P.S. Right now I'm using AGS 2.72)

What is the reason you're using 2.72? That version was more or less okay for the early 2000-ies, but for today it has a horrible system compatibility, you'll be stuck with Windows only, and some things will work slower than they could (game rendering for instance).

Perpetuall

Well, there are a few reasons. For one thing, all the modules I am using were written for 2.72 or earlier, and it's a real pain trying to get them to work with AGS 3. Some of them I can't get to work at all, and I'm not skilled enough to know how to code an alternative. I'm also a lot more familiar with 2.72, since that's what I used back when I was... I can't believe it's been this long, but back when I was 13. I get confused a lot because I can't find things where I think they should be. It's just causing me so much extra work between those two problems that I just decided I might as well just use the old version. At least for this part, anyway. I already started it in AGS 3, but I'm planning on making several "episodes" and adding more content as I go. I'm not sure, I might end up using version 4 the most once I get the hang of it, for all I know.  :tongue:

Crimson Wizard

#3
Quote from: Perpetuall on Sat 27/07/2024 02:47:00Well, there are a few reasons. For one thing, all the modules I am using were written for 2.72 or earlier, and it's a real pain trying to get them to work with AGS 3. Some of them I can't get to work at all, and I'm not skilled enough to know how to code an alternative.

It's possible to use 2.72 scripts in AGS 3, if you toggle some of the backwards compatibility settings:
https://adventuregamestudio.github.io/ags-manual/GeneralSettings.html#backwards-compatibility

There are only few exceptions, such as "global messages" that you can no longer edit in the AGS 3 editor. But all script functions should still work.
I witnessed few times how the people upgraded their ancient projects to the latest AGS 3 without changing much of the script.

Perpetuall

Well, maybe you can help me figure out this one module in particular. If so, I *might* be able to fix the rest of it.
Most of them I have figured out how to get working, but the Scroll Room module is giving me tons of errors. It seems like a silly thing to be stuck on, but I really need it for my outdoor rooms in order to give it an "unlimited space" feel. Definitely not the smoothest solution, but it works. I'm sure there must be a simple way to do the same thing-in fact I read something yesterday about using eNextscreentransitioninstant-but I'm not sure how to do it.
The first error message I get involved AGS_MAX_CHARACTERS, which I know is obsolete now, and I know I found a workaround before, but I forget now what I did. And then I get more error messages after that, but a lot of it is outdated terms being used.

We honestly should update some of these modules for the newer versions and re-release them for other beginners. I bet there are a lot of people out there who would appreciate it. It would probably prevent a lot of future frustration and maybe ignite some more interest in the editor. Most of the modules just give you error messages when you try to download them now. I'm sure that's off-putting to anybody who's new to using AGS and doesn't know how to script.

It's sad when you look back on the games and memories you cherished growing up and get a bunch of error messages every time you try to relive them. That's been happening to me a lot now when ever I try to share one of my old games with my son. Especially a lot of the things available from third party developers, it's just gone now. I always thought technology would make it possible for us to archive our legacies for future generations forever. But it turns out things decay in cyberspace just like everywhere else. I'm surprised nobody took the time to try to preserve these things they worked so hard on. //nostalgic rant

Khris

As for the distance question, the approach primarily depends on the perspective of your graphics. Is it top-down? Sideview? In-between?

Collision checks in a 2D game are usually done via hitboxes; each game loop you calculate a rectangle for the sword based on the player's position, direction they're facing in and animation frame.
The same is done for the enemy's body.
Next you do a rectangle vs rectangle check and if they overlap, you process the hit.

I'd use a struct like:
Code: ags
managed struct Rectangle {
  int x1, y1, x2, y2;
  import boolean Overlaps(Rectangle* other);
};

The function looks like this:
Code: ags
boolean Rectangle::Overlaps(Rectangle* other) {
  if (this.x1 > other.x2 || this.x2 < other.x1) return false;
  if (this.y1 > other.y2 || this.y2 < other.y1) return false;
  return true;
}

You can now use that like:
Code: ags
#define ENEMY_COUNT 10

Rectangle* playerSword;
Rectangle* enemies[ENEMY_COUNT];

void game_start() {
  playerSword = new Rectangle;
  for (int i = 0; i < ENEMY_COUNT; i++) enemies[i] = new Rectangle;
}

void UpdateHitboxes() {
  // very rough example code
  playerSword.x1 = player.x + 5;
  enemy[0].x1 = cEnemy1.x - 10;
  // ...
}

void CheckCollisions() {
  for (int i = 0; i < ENEMY_COUNT; i++) {
    if (playerSword.Overlaps(enemies[i])) {
      // enemy #i just got hit
    }
  }
}

Perpetuall

Wow, thanks Khris. That's amazing how you can churn out these formulas like that.  :-D

SMF spam blocked by CleanTalk