Skip to content

Enemy AI

DLAmeng edited this page Aug 27, 2022 · 6 revisions

Introduction

This page is creating for EnemyAI. EnemyAI sets the movement and attack mode of the enemy, for example, the enemy can attack other units remotely within a certain distance

  • updown and leftright tasks
  • change the entity speed
  • Ranged Attack Task

Usage

1. Creating updown and leftright tasks, These tasks can move the entity horizontally or up and down.

public void update() {
        float position_x = this.owner.getEntity().getPosition().x;
        if (isMove) {
            if(position_x <= p_x + x/2) {
                this.owner.getEntity().getComponent(PhysicsComponent.class).getBody().setLinearVelocity(new Vector2(1,0));
            } else {
                isMove = false;
            }
        } else {
            if(position_x >= p_x - x/2) {
                this.owner.getEntity().getComponent(PhysicsComponent.class).getBody().setLinearVelocity(new Vector2(-1,0));
            } else {
                isMove = true;
            }
        }
    }

2. Change the movement speed of the entity by adding a method to the PhysicsMovementComponent.java file.

public PhysicsMovementComponent(Vector2 speed) {
    maxSpeed = speed;
  }

3. Creating rangedAttack task, this task can control the attack interval, distance, and target of the entity.

This function controls the attack interval.

public void update() {
        if (timeSource.getTime() >= endtime) {
            this.owner.getEntity().getEvents().trigger("attack");
            endtime = timeSource.getTime() + (int)(waitTime * 1000);
        }
    }

This function controls the distance of the entity.

private float DistanceToTarget() {
        return owner.getEntity().getPosition().dst(target.getPosition());
    }

Table of Contents

Home

Game

Game Home

Design Influences

Gameplay Features

Style

Story

Friendly Units
Map
City
Buildings
Unit Selections

Spell

Game User Testing: Theme of Unit Selection & Spell System

UI User Testing

Tutorial

Resource Stats Display

Loading Screen Bar

Health Bars
In Game menu
  • Feature
  • User Testing:In Game Menu

Landscape Tile Design

Landscape Tile Design Feedback

Weather Design

Weather Design Feedback

Camera Movement

Enemy design

Enemy Units

Enemy AI

How Animation Works

Map Flooding

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally