Java 2D OpenGL RPG Tutorial 5: Creating Enemies

preview_player
Показать описание
In this video we add enemies to our game, and implement basic AI for them to chase the player.
Рекомендации по теме
Комментарии
Автор

Aside from an error that took me an hour to fix (It was my fault not yours) I love these so much! Although it is much harder for me because I am taking a side view, making enemy AI a lot harder to make, I would be hopelessly lost without ya. thanks benny.

lightbulbpenguin
Автор

Using a well designed XML to hold all the data you need(specifically stats, their equipment and such) and then using a class to hold all the parameters that the XML would read up(stats, equipment etc).

You could also for instance make a class called: "Creatures" which contain information of all living creatures that can move and be killed(including the player), then use a child class for the player(and potential companions) and enemy containing specific things for them.

SknCommonLisper
Автор

haha yeah didn't notice the time it was posted until after I responded, great about the 100% points thing, good job!

SknCommonLisper
Автор

Glad I could help :), not used XML myself(I only use mysql, but I do mostly swing applications or webpages with drupal and php) but know many big title games used it years ago(if they still do I don't know), such as diablo 2(modding of items were mostly just sitting in the xml files and editing stats).

SknCommonLisper
Автор

As for sprites, you would need a path system that reads up the appropriate sprites for the class using a clever naming system(paths can be static and should be as well for obvious reasons). For instance: (Graphics>Enemies>) and then have stuff like gobling.png, skeleton.png etc. Exactly how to do that depends on what you find better, 2 alternatives could be either 1: using the creature name as parameter for the file, or 2: having a xml column for that.

SknCommonLisper
Автор

Careful with protected keyword, it means the member is visible within the package and subclasses. (not a logical and, but a logical or looks weird here). Use private unless you are sure you need to call it from somewhere else.

tabularasa
Автор

Okay, that sounds a lot like what I want.
I'll go figure out how to do that (didn't do anything with XML yet, but I know how to google up good tutorials), so thanks a lot.

Orillion
Автор

Ok thanks! *offs to finish the full game source code to make sure I don't screw up*

Chillers
Автор

While I'm really new to game programming I would assume it works allot like programming in general. If so you would probably use an enemy class that contain the functions and variables all enemies would have(dying, positions etc), then create child classes for each enemy(containing specified things like stats etc), or a child class for the overall enemy type(humans for instance) then a child class of that child class for specific(soldier, bandits etc).

SknCommonLisper
Автор

hey benny just for information you can use toArray() for sphereCollide just add an array as a parameter like this : return res.toArray(new GameObject[res.size()]);

bkali
Автор

hey, trying to learn from an old tutorial! just to register, i had some fun playing with the chase system, tried to make the movement of the player and the enemy smoother creating a lengthDir method for both x and y axis.
UTIL CLASS
public static double twoPointAngle(float x1, float y1, float x2, float y2) {
return Math.atan2(x2 - x1, y2 - y1);
}

public static double lengthDirX(float x, float angle) {
return x * Math.sin(angle);
}

public static double lengthDirY(float y, float angle) {
return y * Math.cos(angle);
}
MONSTER CLASS
protected void Chase() {
float targetX = getTarget().getX();
float targetY = getTarget().getY();
float pointAngle = (float) Util.twoPointAngle(x, y, targetX, targetY);
float speedX = (float) Util.lengthDirX(getStats().getSpeed(), pointAngle);
float speedY = (float) Util.lengthDirY(getStats().getSpeed(), pointAngle);
x+=speedX*DAMPING;
y+=speedY*DAMPING;
}
PLAYER CLASS
private void move(float xTo, float yTo) {
if(xTo==0&&yTo==0) return;
float pointAngle = (float) Util.twoPointAngle(0, 0, xTo, yTo);
x+=Util.lengthDirX(getSpeed(), pointAngle);
y+=Util.lengthDirY(getSpeed(), pointAngle);
}
I made the player have 4 direction variables that are activated on getInput, and the move runs within the update so it can receive two directions at the same time

sorry for bad english! thanks from brazil

flamingo
Автор

You might want to turn Game into an Singleton, keeping track of it's own instance, that way you can keep the sphereCollide in the Game object as a static method.

tabularasa
Автор

I love this sires, can't wait for the next part!!!

TheLetsplayminecraft
Автор

thanks for replying :) I managed to figure it out myself that time and even got for my video game as a school project 100% points. :-D Thank you very much anyway for effort ;-)

blahri
Автор

Use a condition that checks that both the X position and the Y position is within the area of the item, the problem is that your just checking X(or checking X first without checking Y, I'm not sure since I cant' see your code) example:

if playerX <= itemX AND playerY <= itemY
pick up item.

SknCommonLisper
Автор

Hah, public static return gave me a good laugh

laxeot
Автор

The problem for that is that everything would have to be hard-coded.
I'd find it more practical to just have a databank with lists of what exists and what "stats" it has and which graphics files it uses and so on. Otherwise my project colleagues (who are not programmers, but designers and artists) won't be able to implement and test the stuff they made on their own.

Is there a way to make the program read out folders full of data (or a databank) and create those enemies/maps/items dynamically?

Orillion
Автор

i was following along in c# because i want to see if i could and everything was going fine up until this point.30:56 i am using winform and opentk and the issue is that winfrom complains when you use static variables  or functions i dont know 

simplecastic
Автор

The CookieMonster has a constant. Constants should be static AND final. You've done that before, but not here. Similarly you arbitrarily started beginning methods with capitals.

The problem with the cast is that toArray() returns an Object[], and you can't down cast. If you have an Object, you don't know that it's actually a GameObject anymore. You just know that it's an Object. You need to use res.toArray(new GameObject[0]); which uses Generics and works fine.

No, what nextX and nextY ACTUALLY are is the distance between the CookieMonster and the target in the x, and y axes. They're not a position, they're not a speed. They're a distance.

seigeengine
Автор

Maybe add a method in the stats class called getXP()? (another thought from my prev comment)

SergeantBalthazar
welcome to shbcf.ru