Commit 94f3166d authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

Conversions between pixels and cube coordinates

parent 5da77527
package com.mygdx.game.controllers
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import kotlin.math.sqrt
// https://stackoverflow.com/questions/2459402/hexagonal-grid-coordinates-to-pixel-coordinates
fun cubeToPixel(x: Float, y: Float, z: Float, hexSideLength: Float = 1F): Array<Float> {
val pixelX = sqrt(3F) * hexSideLength * ( (z / 2F) + x )
val pixelY = 3F/2F * hexSideLength * z
return arrayOf(pixelX, pixelY)
}
fun cubeToPixel(vector: Vector3, hexSideLength: Float = 1F): Array<Float> {
val pixelX = sqrt(3F) * hexSideLength * ( (vector.z / 2F) + vector.x )
val pixelY = 3F/2F * hexSideLength * vector.z
return arrayOf(pixelX, pixelY)
}
fun pixelToCube(x: Float, y: Float, hexSideLength: Float= 1F) : Vector3 {
val cubeX = (sqrt(3F) / 3F * x - y / 3F) / hexSideLength
val cubeY = -(sqrt(3F) / 3F * x + y / 3F) / hexSideLength
val cubeZ = 2F / 3F * y / hexSideLength
return Vector3(cubeX, cubeY, cubeZ)
}
fun pixelToCube(vector: Vector2, hexSideLength: Float= 1F) : Array<Float> {
val x = vector.x
val y = vector.y
val cubeX = (sqrt(3F) / 3F * x - y / 3F) / hexSideLength
val cubeY = -(sqrt(3F) / 3F * x + y / 3F) / hexSideLength
val cubeZ = 2F / 3F * y / hexSideLength
return arrayOf(cubeX, cubeY, cubeZ)
}
\ No newline at end of file
import com.mygdx.game.model.GameState
//Logic of the game, holds a GameState, operations
//(((Controller???))) Model can change its own data, but has to be told by controller
class Game(gameState: GameState) {
}
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
import kotlin.math.sqrt
fun cubeToPixel(x: Float, y: Float, z: Float, s: Float = 1F): Array<Float> {
// https://stackoverflow.com/questions/2459402/hexagonal-grid-coordinates-to-pixel-coordinates
val pixelX = sqrt(3F) * s * ( (z / 2F) + x )
val pixelY = 3F/2F * s * z
return arrayOf(pixelX, pixelY)
}
fun cubeToPixel(vector: Vector3, s: Float = 1F): Array<Float> {
// https://stackoverflow.com/questions/2459402/hexagonal-grid-coordinates-to-pixel-coordinates
val pixelX = sqrt(3F) * s * ( (vector.z / 2F) + vector.x )
val pixelY = 3F/2F * s * vector.z
return arrayOf(pixelX, pixelY)
}
\ No newline at end of file
......@@ -8,6 +8,6 @@ abstract class AbstractRules {
abstract var moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How far a piece can jump (over other pieces)
abstract fun getPlayerStartfields(player: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetfields(player: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun getPlayerStartfields(playerId: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerId: Int) : List<Vector3> // Returns a list of targetfields for a given player
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.mygdx.game.model.gamemodes.rules
import com.badlogic.gdx.math.Vector3
import com.mygdx.game.model.AbstractRules
import com.mygdx.game.model.Field
class DefaultRules: AbstractRules() {
// Sequence: top, upperright, lowerright, bottom, lowerleft, upperleft
......@@ -9,12 +10,12 @@ class DefaultRules: AbstractRules() {
override var moveRange = 1
override var jumpRange = 2
override fun getPlayerStartfields(player: Int): List<Vector3> {
return startFields[player]
override fun getPlayerStartfields(playerId: Int): List<Vector3> {
return startFields[playerId]
}
override fun getPlayerTargetfields(player: Int): List<Vector3> {
var index = player
override fun getPlayerTargetFields(playerId: Int): List<Vector3> {
var index = playerId
for (i in 0..2) {
index++
......
......@@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.mygdx.game.controllers.PlayerController;
......@@ -15,14 +16,20 @@ import com.mygdx.game.controllers.PlayerController;
public class PlayView extends View{
private Texture background;
private Image backgroundImage;
private Sound gameLoop;
private Sound transitionFX;
int Help_Guides = 12;
int row_height = Gdx.graphics.getHeight() / Help_Guides;
int col_width = Gdx.graphics.getWidth() / Help_Guides;
BitmapFont font;
protected PlayView(GameViewManager gvm, PlayerController playerController, AssetManager assetManager, Stage stage, Skin skin) {
super(gvm, playerController, assetManager, stage, skin);
background = new Texture ("Game/GameBoard/1x/GameBoard@1x.png");
background = new Texture ("Game/GameBoard/0.25x/GameBoard@0.25x.png");
this.backgroundImage = new Image(background);
backgroundImage.setPosition(col_width*6-backgroundImage.getWidth()/2, row_height*6-backgroundImage.getHeight()/2);
stage.addActor(backgroundImage);
font = new BitmapFont();
......@@ -35,6 +42,12 @@ public class PlayView extends View{
@Override
protected void handleInput() {
if(Gdx.input.isTouched()) {
int x = Gdx.input.getX() - (Gdx.graphics.getWidth() / 2);
int y = Gdx.input.getY() - (Gdx.graphics.getHeight() / 2);
System.out.println(x + ", " + y);
}
if(Gdx.input.isKeyPressed(Input.Keys.ESCAPE)){
}
......
......@@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CheckersServer.iml" filepath="$PROJECT_DIR$/.idea/CheckersServer.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/CheckersServer.iml" filepath="$PROJECT_DIR$/.idea/modules/CheckersServer.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/android/CheckersServer.android.iml" filepath="$PROJECT_DIR$/.idea/modules/android/CheckersServer.android.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/core/CheckersServer.core.iml" filepath="$PROJECT_DIR$/.idea/modules/core/CheckersServer.core.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/desktop/CheckersServer.desktop.iml" filepath="$PROJECT_DIR$/.idea/modules/desktop/CheckersServer.desktop.iml" />
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment