Game AI & Unity/Java Steering game

[Game AI][Steering Behavior] 7. engine.GameObject.java

bay07 2024. 3. 20. 12:19

1. 전체적인 구조

src > engine > GameObject.java 

 

  추상 메서드 (Abstract Method) 일반 메서드 (Concrete Method)
구현 여부 선언 O, 구현 X
서브 클래스에서 구현해야함
선언 O, 구현 O
해당 클래스에서 직접 호출될 수 있다
호출 방식 하위 클래스에서 오버라이딩 하여 구현하기 
서브 클래스의 독특한 동작을 정의할 수 있다
해당 클래스의 인스턴스에서 직접 호출가능
사용 용도  여러 클래스에서 공통적으로 가져야하는 동작이지만, 각 클래스마다 구현이 달라야하는 경우
추상 클래스에서 일반적인 동작을 정의하고, 서브 클래스에서 이것을 구체화한다 
특정 클래스에서 공통적으로 사용되는 동작을 정의함. 해당 클래스에서 직접 호출되어야 하며, 상속을 통해 수정되거나 확장될 수 있다

 

 

1. 멤버 변수 

double m_x, m_y  : 게임 객체의 위치를 나타내는 x좌표, y좌표 

 

2. 추상 메서드

   
public abstract void update(Game game, double delta_t) 게임 객체의 상태를 업데이트 함
delta_t 는 경과한 시간
public abstract void draw(Graphics2D g) 게임 객체를 화면에 그림 
public abstract RotatedRectangle getCollisionBox() 게임 객체의 충돌 상자를 반환함
충돌 상자는 객체 간 충돌을 감지하는데 사용된다 

 

3. 일반 메서드

   
public double getX(), public double getY() 게임 객체의 x좌표, y좌표를 반환함
public boolean collision(GameObject o)
public boolean collision(RotatedRectangle r)
충돌 여부 검사
(다른 게임 객체나 회전된 사각형에 충돌했을 때)
충돌 상자를 사용하여 충돌을 감지한다

 

4. 주석

게임 객체가 가져야하는 최소한의 요구사항

게임 객체는 update, draw, getCollisionBox 함수를 정의해야한다. 

이것을 통해서 객체의 동작, 그리기, 충돌 상자를 지정할 수 있다.

 

5. 결론

GameObject 클래스는 모든 게임 객체가 가져야하는 기본적인 속성과 동작을 추상화한 클래스이다.

이것을 상속하여 다양한 종류의 게임 객체를 구현할 수 있다. 

 

 

- 전체적인 코드 

더보기
package engine;

import java.awt.Graphics2D;

/**
 *
 * @author santi
 */
public abstract class GameObject {
    double m_x,m_y;
    
    /*
    The only thing we require of game objects is that they define the following three functions:
    - update: that contains their bhavior
    - draw: that draws them in the game window
    - getCollisionBox: that tells the game engine where they physically are in th game space
    */
    public abstract void update(Game game, double delta_t);
    public abstract void draw(Graphics2D g);
    public abstract RotatedRectangle getCollisionBox();

    
    public double getX() {
        return m_x;
    }
    
    public double getY() {
        return m_y;
    }

    public boolean collision(GameObject o) {
        if (getCollisionBox()==null || o.getCollisionBox()==null) return false;
        return RotatedRectangle.RotRectsCollision(getCollisionBox(), o.getCollisionBox());
    }

    public boolean collision(RotatedRectangle r) {
        if (getCollisionBox()==null || r==null) return false;
        return RotatedRectangle.RotRectsCollision(getCollisionBox(), r);
    }
}