I wrote an event system for a game engine I'm working on, which adds en event to a tile in a map which when the player intersects it, it does something. Whatever that something is. In this test case, it loads a new map.
The problem is that the frame rate when starting the game is about 30 right up until I begin the event by walking over it, which starts the event and brings the fps up to about 60.
EDIT: it seems that this issue only popped up after I added events. However, I added another similar warping event to see if the fps dropped even more, it didn't. And after I finished the first event, the fps went to normal and I still used the second event fine.
Here is the event class:
public class Event {
//Warping variables
private int newX, newY;
private Map _map, _mapOverlay;
private String eventType;
private String _name;
public Event(String name, String type) {
_name = name;
eventType = type;
}
public void setWarp(Map map, Map overlay, int x, int y) {
_map = map;
_mapOverlay = overlay;
newX = x;
newY = y;
}
public Map getMap() { return _map; }
public Map getOverlay() { return _mapOverlay; }
public int getNewX() { return newX; }
public int getNewY() { return newY; }
public String getEventType() { return eventType; }
public String getname() { return _name; }
}
This is the algorithm that is called when a warp event is collided with:
if(spr.spriteType() == "player" && tile.hasEvent()) {
if(tile.event().getEventType() == "warp") {
tiles().clear();
currentMap = tile.event().getMap();
currentOverlay = tile.event().getOverlay();
for(int i = 0; i < currentMap.getWidth() * currentMap.getHeight(); i++){
tiles().add(currentMap.accessTile(i));
tiles().add(currentOverlay.accessTile(i));
}
playerX = tile.event().getNewX();
playerY = tile.event().getNewY();
tile.endEvent();
}
}
This is the section inside of the tile class which contains methods that have to do with adding an event.
public void addEvent(Event event) {
this.event = event;
hasEvent = true;
}
public Event event() { return event; }
public boolean hasEvent() { return hasEvent; }
public void endEvent() {
event = null;
hasEvent = false;
}
Lastly, here are the three lines used to add an event to an existing tile in a map:
warp1 = new Event("testWarp", "warp");
warp1.setWarp(city, cityOverlay, -200, 0);
city.accessTile(204).addEvent(warp1);