0
\$\begingroup\$

I would like to insert random enemies in the map in different positions. It's possible?

This is how i load image of enemy: Enemy.java:

public class Enemy {
    public Array<Sprite> enemy = new Array<Sprite>(2);  

    public void nemici() {
        Sprite mysterio;
        Sprite boss;
        enemy.add(mysterio = new Sprite(new Texture("mysterio.png")));
        enemy.add(boss = new Sprite(new Texture("boss.png")));
    }
}

And this is how i draw with my level1.java:

public class Level1 implements Screen{
    
    private Texture BackgroundLevel1;
    
    NameGame game;
    
    Sprite card;
    private Enemy enemies = new Enemy();
    
    public Level1 (NameGame game){
        this.game = game;
        
        enemies.nemici();
        card = enemies.enemy.random();
        
        BackgroundLevel1 = new Texture("BackgroundLevel1.png");
    }
    
    @Override
    public void show() {
    }

    @Override
    public void render(float f) {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        game.batch.begin();
        
        //Background
        game.batch.draw(BackgroundLevel1, 0, 0, NameGame.WIDTH, NameGame.HEIGHT);

        game.batch.draw(card,200,0,100,100);  //Example
        
        game.batch.end();
    }
\$\endgroup\$
5
  • \$\begingroup\$ I'm not sure where you think your limitations are for achieving this. When you generate the random enemy, you also generate the random position. We know nothing about your game so it's hard to give you precise details, but you could have a set of predefined possible positions associated with your level, and you randomly pick one of those. \$\endgroup\$
    – Vaillancourt
    May 18, 2022 at 11:51
  • \$\begingroup\$ Do you mean an array of positions to put a random enemy in a random position?? \$\endgroup\$
    – witsel
    May 18, 2022 at 12:11
  • \$\begingroup\$ I think the description of the issue could be improved. What prevents you from adding the enemy at random position? \$\endgroup\$
    – Vaillancourt
    May 18, 2022 at 12:54
  • \$\begingroup\$ And yes I meant that. e.g. in public Level1 (NameGame game) you create a collection of positions where an enemy could spawn, then randomly pick one of those positions. Then again, that may make sense, or that may not make sense, depending on your game. \$\endgroup\$
    – Vaillancourt
    May 18, 2022 at 12:58
  • \$\begingroup\$ I wrote this question because I don't know how to put multiple enemies on screen in certain locations. The coordinates are constructed from 4 values (x, y, width, height) how can they be inserted into a variable? Could you give me an example so I can try it on my code? \$\endgroup\$
    – witsel
    May 18, 2022 at 15:41

1 Answer 1

1
\$\begingroup\$

The idea is to store the information of an enemy into a class instance. Here is the pseudo-code of what I have in mind.

I've added pseudo-comments to the pseudo-code to explain what's going on.

/**
 * This holds an enemy.
 */
class Enemy:
  Sprite sprite_;
  Vec2 size_;
  Vec2 position_;
  
  func Enemy(string spritePath, Vec2 size): // constructor
    sprite_ = new Texture(spritePath);
    size_ = size;
  
  
/**
 * You store all of the potential enemies here. All the enemies of the same type
 * share the same values for sprite and size. We don't fill the position 
 * because that is the level that will load it.
 */
static class EnemyFactory:
  Array<Enemy> enemies_;
  
  func EnemyFactory(): // constructor
    enemies_.add(new Enemy( "mysterio.png", new Vec2( 100,100 ) );
    enemies_.add(new Enemy( "boss.png", new Vec2( 100,100 ) );
  
  func GenerateOne(Vec2 position):
    Enemy newEnemy = enemies_.random().clone();
    newEnemy.position_ = position;
    return newEnemy;


class Level1:
  Array<Enemy> enemies_;
  
  func Level1(): // constructor
    ...
    // Start by defining all the potential spots where an enemy could spawn.
    // Ideally, you'll want to put this in an external file and load it when the
    // game starts. 
    Array<Vec2d> potentialPositions;
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    potentialPositions.add( new Vec2d(..., ...) )
    
    // Create your enemies. Your enemy types have been defined in the 
    // EnemyFactory, so it's only a matter of selecting one and placing it using
    // what you've previously defined. 
    //
    // Note that we remove the positions once we've used it because we don't 
    // want more than one enemy at the same spot
    Vec2d enemyPosition = potentialPositions.random();
    potentialPositions.erase( enemyPosition ); 
    enemies_.add(EnemyFactory.GenerateOne( enemyPosition );
    
    enemyPosition = potentialPositions.random();
    potentialPositions.erase( enemyPosition );
    enemies_.add(EnemyFactory.GenerateOne( enemyPosition );

    enemyPosition = potentialPositions.random();
    potentialPositions.erase( enemyPosition );
    enemies_.add(EnemyFactory.GenerateOne( enemyPosition );
    ...
    
    
  func Render():
    ...
    for enemy in enemies_:
      // The data you need is stored in your class instance
      game.batch.draw(enemy.sprite_,enemy.position_,enemy.size_);
      
    ...
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Thanks, it's very helpful. Sorry for my bad english \$\endgroup\$
    – witsel
    May 18, 2022 at 19:58
  • \$\begingroup\$ Your English is fine :) \$\endgroup\$
    – Vaillancourt
    May 18, 2022 at 21:11
  • \$\begingroup\$ I can draw an enemy in one position. But when he writes the next one, the previous one disappears. \$\endgroup\$
    – witsel
    May 19, 2022 at 10:31
  • 1
    \$\begingroup\$ In GenerateOne, as described above, did you clone the object? I don't use Java, but AFAIK, if you just do an assignation, you're just getting the reference, and so it means you only have one instance, and so it means that if you change the coordinates of the second one, it changes the coordinate only of the one instance... \$\endgroup\$
    – Vaillancourt
    May 19, 2022 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .