forked from andybalaam/rabbit-escape
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] World 이벤트 관리에 Observer 패턴 적용 #13
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
164aff4
[feat] Added EventType enum class
yymin1022 56aa40f
[feat] Added GameEvent Interface
yymin1022 9742331
[feat] Added RabbitEvent based on GameEvent
yymin1022 6fcdfd3
[feat] Added StatsChangedEvent based on GameEvent
yymin1022 f58dd19
[feat] Added GameEventListener Interface
yymin1022 24cbdae
[feat] Added GameEventManager Class
yymin1022 d12e32c
[feat] Added WorldStatsListenerAdapter based on Original Listener
yymin1022 7cb5d81
[feat] Applied new WorldStatsListener for World
yymin1022 c3cee2f
[fix] GameEventListener Type Converting
yymin1022 663da86
[test] Added Unit-Test for GameEventListener
yymin1022 abac5a9
[feat] Added LevelWinEvent Class
yymin1022 483582b
[feat] Added LevelWinListenerAdapter Class based on LevelWinListener
yymin1022 2ed9915
[feat] Applied new LevelWinListener for World
yymin1022 aa22804
[test] Added Unit-Test for LevelWinListener
yymin1022 b4aeb96
[fix] LevelWinListener Type Converting
yymin1022 80df142
[test] Added Unit-Test for LevelWinListener
yymin1022 69bedbd
[test] Improved WorldListener Tests
yymin1022 fc9a5b6
[test] Fixed TestGameEvent Constructor
yymin1022 3a7e139
[test] Fixed TestGameEvent Constructor
yymin1022 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package rabbitescape.engine.events; | ||
|
||
public enum EventType { | ||
RABBIT_MOVED, | ||
RABBIT_SAVED, | ||
RABBIT_KILLED, | ||
TOKEN_ADDED, | ||
TOKEN_USED, | ||
BLOCK_CHANGED, | ||
WATER_CHANGED, | ||
LEVEL_WON, | ||
LEVEL_LOST, | ||
STATS_CHANGED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import java.util.Map; | ||
|
||
public interface GameEvent { | ||
EventType getType(); | ||
Map<String, Object> getEventData(); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/engine/src/rabbitescape/engine/events/GameEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package rabbitescape.engine.events; | ||
|
||
public interface GameEventListener { | ||
void onGameEvent(GameEvent event); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/engine/src/rabbitescape/engine/events/GameEventManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import java.util.*; | ||
|
||
public class GameEventManager { | ||
private final Map<EventType, List<GameEventListener>> listeners; | ||
|
||
public GameEventManager() { | ||
this.listeners = new HashMap<>(); | ||
for (EventType type : EventType.values()) { | ||
listeners.put(type, new ArrayList<GameEventListener>()); | ||
} | ||
} | ||
|
||
public void addEventListener(EventType type, GameEventListener listener) { | ||
List<GameEventListener> typeListeners = listeners.get(type); | ||
if (!typeListeners.contains(listener)) { | ||
typeListeners.add(listener); | ||
} | ||
} | ||
|
||
public void removeEventListener(EventType type, GameEventListener listener) { | ||
List<GameEventListener> typeListeners = listeners.get(type); | ||
typeListeners.remove(listener); | ||
} | ||
|
||
public void fireEvent(GameEvent event) { | ||
List<GameEventListener> typeListeners = listeners.get(event.getType()); | ||
for (GameEventListener listener : typeListeners) { | ||
listener.onGameEvent(event); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/engine/src/rabbitescape/engine/events/LevelWinEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class LevelWinEvent implements GameEvent { | ||
private final EventType type; | ||
private final boolean won; | ||
|
||
public LevelWinEvent(boolean won) { | ||
this.type = won ? EventType.LEVEL_WON : EventType.LEVEL_LOST; | ||
this.won = won; | ||
} | ||
|
||
@Override | ||
public EventType getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getEventData() { | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("won", won); | ||
return data; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/engine/src/rabbitescape/engine/events/LevelWinListenerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import rabbitescape.engine.LevelWinListener; | ||
|
||
public class LevelWinListenerAdapter implements LevelWinListener { | ||
private final LevelWinListener originalListener; | ||
private final GameEventManager eventManager; | ||
|
||
public LevelWinListenerAdapter(LevelWinListener originalListener, GameEventManager eventManager) { | ||
this.originalListener = originalListener; | ||
this.eventManager = eventManager; | ||
} | ||
|
||
@Override | ||
public void won() { | ||
// 기존 리스너 호출 | ||
if (originalListener != null) { | ||
originalListener.won(); | ||
} | ||
|
||
// 새로운 이벤트 발생 | ||
eventManager.fireEvent(new LevelWinEvent(true)); | ||
} | ||
|
||
@Override | ||
public void lost() { | ||
// 기존 리스너 호출 | ||
if (originalListener != null) { | ||
originalListener.lost(); | ||
} | ||
|
||
// 새로운 이벤트 발생 | ||
eventManager.fireEvent(new LevelWinEvent(false)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/engine/src/rabbitescape/engine/events/RabbitEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import rabbitescape.engine.Rabbit; | ||
import rabbitescape.engine.util.Position; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RabbitEvent implements GameEvent { | ||
private final EventType type; | ||
private final Rabbit rabbit; | ||
private final Position position; | ||
|
||
public RabbitEvent(EventType type, Rabbit rabbit, Position position) { | ||
this.type = type; | ||
this.rabbit = rabbit; | ||
this.position = position; | ||
} | ||
|
||
@Override | ||
public EventType getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getEventData() { | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("rabbit", rabbit); | ||
data.put("position", position); | ||
return data; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/engine/src/rabbitescape/engine/events/StatsChangedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class StatsChangedEvent implements GameEvent { | ||
private final int numSaved; | ||
private final int numToSave; | ||
|
||
public StatsChangedEvent(int numSaved, int numToSave) { | ||
this.numSaved = numSaved; | ||
this.numToSave = numToSave; | ||
} | ||
|
||
@Override | ||
public EventType getType() { | ||
return EventType.STATS_CHANGED; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getEventData() { | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("numSaved", numSaved); | ||
data.put("numToSave", numToSave); | ||
return data; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/engine/src/rabbitescape/engine/events/WorldStatsListenerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package rabbitescape.engine.events; | ||
|
||
import rabbitescape.engine.WorldStatsListener; | ||
|
||
public class WorldStatsListenerAdapter implements WorldStatsListener { | ||
private final WorldStatsListener originalListener; | ||
private final GameEventManager eventManager; | ||
|
||
public WorldStatsListenerAdapter(WorldStatsListener originalListener, GameEventManager eventManager) { | ||
this.originalListener = originalListener; | ||
this.eventManager = eventManager; | ||
} | ||
|
||
@Override | ||
public void worldStats(int numSaved, int numToSave) { | ||
// 기존 리스너 호출 | ||
if (originalListener != null) { | ||
originalListener.worldStats(numSaved, numToSave); | ||
} | ||
|
||
// 새로운 이벤트 발생 | ||
eventManager.fireEvent(new StatsChangedEvent(numSaved, numToSave)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분이 옵저버 패턴에서 옵저버들을 업데이트 하는 부분이군요! 옵저버들의 세부 구현체가 아닌 인터페이스를 참조해서 잘 구현된 것 같습니다.