0

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);
  • Use `equals` when comparing strings, never `==`. – M A Nov 08 '14 at 21:30
  • alright Ill start with that – Travis Dewitt Nov 08 '14 at 21:31
  • Tip: there is a stack site devoted to game development which is awesome here: http://gamedev.stackexchange.com/ And regarding your question, I was unable to read thoroughly through the code, but my initial question is: are you checking every tile every frame for a hit? – Mr. Developerdude Nov 08 '14 at 21:34
  • Yes I am, should I not be? I guess it does sound intense. The engine is sill young, this will most likely change, also thank you for the link. – Travis Dewitt Nov 08 '14 at 21:36
  • I mean, maybe you should test the tile you are on and the 8 surrounding it, not the entire map – Mr. Developerdude Nov 08 '14 at 21:36
  • My initial thought was that it was easy at the time to do it this way, I'll end up changing it to do it that way, and Ill hook that to npc's as well. – Travis Dewitt Nov 08 '14 at 21:38
  • Depending on the number / size of maps and available memory, you could preload the maps using a background thread - that way you can swap maps using a couple of operations rather than having to iterate through the entire map – Zim-Zam O'Pootertoot Nov 08 '14 at 22:29
  • the game is planned on being massive, an adventure rpg, which means I think that may not work. – Travis Dewitt Nov 08 '14 at 22:39

0 Answers0