Created
October 8, 2020 16:26
-
-
Save Mr00Anderson/2f95f96f561b4a6de6ed01a54823915c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.virtual_hex.gdx.engine.maps; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.utils.viewport.Viewport; | |
/** | |
* Screen x,y - Screen coordinates | |
* World x,y - World coordinates | |
* World Pos x,y - Specific map point - (world x / (texture-width/height)) | |
* Chunk x,y - Chunk coordinates | |
* Chunk Pos x,y - Local Chunk Coordinates | |
*/ | |
public class ChunkedMapPos { | |
public Vector2 temp; | |
// Changed since last frame? | |
public float screenX; | |
public float screenY; | |
public float worldX; | |
public float worldY; | |
public int worldPosX; | |
public int worldPosY; | |
public int chunkX; | |
public int chunkY; | |
public int chunkPosX; | |
public int chunkPosY; | |
public ChunkedMapPos() { | |
temp = new Vector2(); | |
} | |
public ChunkedMapPos screenToAll(int screenX, int screenY, Viewport viewport, int chunkWidthInPoints, int chunkHeightInPoints, int tileWidth, int tileHeight){ | |
convertScreenToWorld(screenX, screenY, viewport); | |
convertMapToPos(tileWidth, tileHeight); | |
convertWorldToChunkPos(chunkWidthInPoints, chunkHeightInPoints); | |
convertWorldToLocalChunkPos(chunkWidthInPoints, chunkHeightInPoints); | |
return this; | |
} | |
public ChunkedMapPos worldToAll(int worldPosX, int worldPosY, int chunkWidthInPoints, int chunkHeightInPoints){ | |
this.worldPosX = worldPosX; | |
this.worldPosY = worldPosY; | |
convertWorldToChunkPos(chunkWidthInPoints, chunkHeightInPoints); | |
convertWorldToLocalChunkPos(chunkWidthInPoints, chunkHeightInPoints); | |
return this; | |
} | |
public ChunkedMapPos convertScreenToWorld(int screenX, int screenY, Viewport viewport) { | |
this.screenX = screenX; | |
this.screenY = screenY; | |
temp.x = screenX; | |
temp.y = screenY; | |
Vector2 worldCoordinates = viewport.unproject(temp); | |
worldX = worldCoordinates.x; | |
worldY = worldCoordinates.y; | |
return this; | |
} | |
public ChunkedMapPos convertMapToPos(int widthInPoints, int heightInPoints){ | |
worldPosX = (int) (worldX / widthInPoints); | |
worldPosY = (int) (worldY / heightInPoints); | |
return this; | |
} | |
public ChunkedMapPos convertWorldToChunkPos(int chunkWidthInPoints, int chunkHeightInPoints){ | |
chunkX = worldPosX / chunkWidthInPoints; | |
chunkY = worldPosY / chunkHeightInPoints; | |
return this; | |
} | |
public ChunkedMapPos convertWorldToLocalChunkPos(int chunkWidthInPoints, int chunkHeightInPoints){ | |
chunkPosX = (worldPosX % chunkWidthInPoints); | |
chunkPosY = (worldPosY % chunkHeightInPoints); | |
return this; | |
} | |
@Override | |
public String toString() { | |
return "ChunkedMapPos{" + | |
"temp=" + temp + | |
", screenX=" + screenX + | |
", screenY=" + screenY + | |
", worldX=" + worldX + | |
", worldY=" + worldY + | |
", worldPosX=" + worldPosX + | |
", worldPosY=" + worldPosY + | |
", chunkX=" + chunkX + | |
", chunkY=" + chunkY + | |
", chunkPosX=" + chunkPosX + | |
", chunkPosY=" + chunkPosY + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment