1. ECS(Entity-Component-System)란 무엇인가
ECS는 데이터(Entity + Component)와 로직(System)을 완전히 분리하는 아키텍처 기법이다.
전통적인 객체지향(OOP)의 상속·다형성 중심 구조와 달리, ECS는 구성(Composition)과 데이터 중심 설계(Data-Oriented Design)를 핵심으로 한다.
한 문장으로 정리하면 다음과 같다.
“Entity는 ID일 뿐이고, 데이터는 Component에 있으며, 로직은 System이 처리한다.”
2. ECS의 3가지 핵심 요소
1) Entity
- 정체성(ID)만 가진다
- 로직도 없고 데이터도 없다
- 단순히 “이것은 하나의 객체다”를 나타내는 식별자다
Player = Entity(1)
Monster = Entity(2)
2) Component
- 순수 데이터 덩어리다
- 행동(메서드)을 가지지 않는다
- Entity에 “붙었다 / 떨어졌다” 개념으로 구성된다
예:
- Position
- Velocity
- Health
3) System
- 로직 담당자다
- 특정 Component 조합을 가진 Entity들만 골라서 처리한다
- 데이터는 직접 소유하지 않는다
예:
- MovementSystem → Position + Velocity
- DamageSystem → Health
3. ECS의 기본 처리 흐름
Entity 생성
↓
Component 부착
↓
System이 필요한Component 조합을 가진Entity 조회
↓
Component 데이터 처리
👉 Entity는 직접 행동하지 않는다
👉 System이 데이터를 읽고 계산한다
4. 왜 ECS를 사용하는가
기존 OOP 구조에서는 다음 문제가 자주 발생한다.
- 상속 구조가 깊어진다
- Player, Monster, NPC 클래스가 비대해진다
- 새로운 행동 추가 시 기존 클래스 수정이 많아진다
ECS는 이를 다음 방식으로 해결한다.
- 상속 대신 조합
- 로직 분리로 결합도 감소
- 데이터 중심으로 성능 최적화에 유리
5. ECS의 장점
- 확장성이 매우 뛰어나다
- 런타임 조합이 자유롭다
- 캐시 친화적 구조로 성능이 좋다
- 대규모 객체 관리에 유리하다
6. ECS의 단점
- 구조가 처음엔 직관적이지 않다
- 소규모 프로젝트에는 과설계가 될 수 있다
- 디버깅이 익숙하지 않으면 어렵다
7. ECS 간단 예제
import java.util.*;
/*
====================================================
Entity
====================================================
*/
class Entity {
private final int id;
public Entity(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
/*
====================================================
Component (Data Only)
====================================================
*/
class Position {
public float x;
public float y;
public Position(float x,float y) {
this.x = x;
this.y = y;
}
}
class Velocity {
public float dx;
public float dy;
public Velocity(float dx,float dy) {
this.dx = dx;
this.dy = dy;
}
}
/*
====================================================
Component Storage
====================================================
*/
class ComponentStore {
public Map<Integer, Position> positions =newHashMap<>();
public Map<Integer, Velocity> velocities =newHashMap<>();
}
/*
====================================================
System
====================================================
*/
class MovementSystem {
public void update(ComponentStore store) {
for (Integer entityId : store.positions.keySet()) {
if (!store.velocities.containsKey(entityId)) {
continue;
}
Position pos= store.positions.get(entityId);
Velocity vel= store.velocities.get(entityId);
pos.x += vel.dx;
pos.y += vel.dy;
}
}
}
/*
====================================================
Main (World)
====================================================
*/
public class ECSExample {
public static void main(String[] args) {
ComponentStore store=new ComponentStore();
MovementSystem movementSystem=new MovementSystem();
Entity player=new Entity(1);
store.positions.put(player.getId(),new Position(0,0));
store.velocities.put(player.getId(),new Velocity(1,1));
movementSystem.update(store);
Position p= store.positions.get(player.getId());
System.out.println("Player Position: " + p.x +", " + p.y);
}
}
8. 이 예제에서 중요한 포인트
- Entity는 ID만 가진다
- Component는 데이터만 가진다
- System만이 로직을 수행한다
- Entity 안에
update()같은 메서드는 없다
9. OOP vs ECS 핵심 차이
구분 | OOP | ECS |
중심 | 객체 | 데이터 |
확장 | 상속 | 조합 |
로직 위치 | 객체 내부 | System |
유연성 | 중간 | 매우 높음 |
성능 최적화 | 어려움 | 유리 |
10. 정리
ECS는
“객체를 행동의 주체로 보지 않고, 데이터를 처리 대상으로 보는 구조”다.
- 대규모 객체 관리에 적합하다
- 게임·시뮬레이션·실시간 시스템에서 강력하다
- 작은 프로젝트에는 부담이 될 수 있다
👉 ID(Entity) + 데이터(Component) + 로직(System)
👉 이 3가지를 철저히 분리하는 사고방식이 ECS다.
Share article