1. 전체적인 위치 살펴보기
src > engine > Game.java
2. 멤버 변수
List<GameObject> m_objects | 게임 내 모든 객체를 저장하는 리스트 게임에 등장하는 모든 객체들이 포함됨 ( 자동차, 장애물 등 ) |
double m_width, m_height | 게임 화면의 가로, 세로 크기 초기화 프레임 속도 초기화 |
int m_frames_per_second | 게임이 몇 프레임으로 동작할 지 나타내는 변수 |
3. 생성자
public Game(double width, double height, int frames_per_second)
게임 객체를 초기화하는 생성자
게임 화면의 가로, 세로, 프레임 속도를 매개변수로 받아서 초기화한다ㅏ
4. 주요 메서드
public void add(GameObject o) | 새로운 객체를 추가 |
public void remove(GameObject o) | 객체 제거 |
public void update() | 게임 상태 업데이트 |
public void draw(Graphics2D g) | 게임 화면 그리기 |
public GameObject collision(GameObject o) | 객체와 충돌하는 다른 객체 찾기 |
public GameObject collision(RotatedRectangle r) | 회전된 사각형과 충돌하는 객체 찾기 |
5. 주석의 설명
실제 게임 엔진을 개발할 때는, 좀 더 효율적인 충돌 검사 알고리즘을 사용해야한다는 설명.
- 전체적인 코드
더보기
package engine;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author santi
*/
public class Game {
/*
In this very simple game engine, the only thing that we need to store is
the list of game objects (Car or obstacles), the dimeions of the map, and
the desired frames per second at which we want to run.
*/
List<GameObject> m_objects = new ArrayList<>();
double m_width, m_height;
int m_frames_per_second;
public Game(double width, double height, int frames_per_second) {
m_width = width;
m_height = height;
m_frames_per_second = frames_per_second;
}
public double getWidth() {
return m_width;
}
public double getHeight() {
return m_height;
}
public int getFPS() {
return m_frames_per_second;
}
public void add(GameObject o) {
m_objects.add(o);
}
public void remove(GameObject o) {
m_objects.remove(o);
}
// check for collisions:
// note: this is an awfully inefficient method, if you ever do a real
// game engine, you need to use a quad-tree or oct-tree to speed
// this up.
public GameObject collision(GameObject o) {
for(GameObject o2:m_objects) {
if (o2!=o && o.collision(o2)) return o2;
}
return null;
}
public GameObject collision(RotatedRectangle r) {
for(GameObject o2:m_objects) {
if (o2.collision(r)) return o2;
}
return null;
}
/*
The bottom two are the two main functions of the game engine:
- update calls the "update" method of all the game objects, which will
trigger their movement.
- draw calls the "draw" method of all the game objects, to display them
in the game window.
*/
public void update() {
double delta_t = 1.0 / m_frames_per_second;
for(GameObject go:m_objects) go.update(this, delta_t);
}
public void draw(Graphics2D g) {
for(GameObject go:m_objects) go.draw(g);
}
}
'Game AI & Unity > Java Steering game' 카테고리의 다른 글
[Game AI][Steering Behavior] 8. engine.GameWindow (0) | 2024.03.20 |
---|---|
[Game AI][Steering Behavior] 7. engine.GameObject.java (0) | 2024.03.20 |
[Game AI][Steering Behavior] 5. engine.Car.java (0) | 2024.03.20 |
[Game AI][Steering Behavior] 4. engine.Car.java (0) | 2024.03.20 |
[Game AI][Steering Behavior] 3. controllers (0) | 2024.03.20 |