Skip to content
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

Syncing of NPC #4

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Implemented different SpritePatch for multiplayer
neohwei committed Nov 12, 2017
commit c31d1e066d1691fe8fe7d514402f1a5d69113bd5
12 changes: 6 additions & 6 deletions core/src/main/scala/my/game/pkg/client/Client.scala
Original file line number Diff line number Diff line change
@@ -34,8 +34,8 @@ class Client(ipAddress:String, port:String, game:Distributedlibgdx2dgame){
* @param x:Float X position of player
* @param y:Float Y position of player
*/
def standStill(x:Float, y:Float){
game.gameUUID.foreach{uuid => clientActor ! StandStill(uuid, MainGameScreen.mapMgr.currentMapName, x, y)}
def standStill(patch:Int, x:Float, y:Float){
game.gameUUID.foreach{uuid => clientActor ! StandStill(uuid, patch, MainGameScreen.mapMgr.currentMapName, x, y)}
}

/**
@@ -45,8 +45,8 @@ class Client(ipAddress:String, port:String, game:Distributedlibgdx2dgame){
* @param x:Float X position of player starting point
* @param y:Float Y position of player starting point
*/
def changeMap(mapFrom:String, mapTo:String, x:Float, y:Float){
game.gameUUID.foreach{uuid => clientActor ! ChangeMap(uuid, mapFrom, mapTo, x, y)}
def changeMap(patch:Int, mapFrom:String, mapTo:String, x:Float, y:Float){
game.gameUUID.foreach{uuid => clientActor ! ChangeMap(uuid, patch, mapFrom, mapTo, x, y)}
}

/**
@@ -58,10 +58,10 @@ class Client(ipAddress:String, port:String, game:Distributedlibgdx2dgame){
* @param direction:Direction Direction of the player
* @param state:State State of the player
*/
def update(delta:Float, map:String, x:Float, y:Float, direction:Direction, state:State, frameTime:Float){
def update(delta:Float, patch:Int, map:String, x:Float, y:Float, direction:Direction, state:State, frameTime:Float){
timeToPing -= delta
if(timeToPing <= 0){
game.gameUUID.foreach{uuid => clientActor ! Alive(uuid, map, x, y, direction, state, frameTime)}
game.gameUUID.foreach{uuid => clientActor ! Alive(uuid, patch, map, x, y, direction, state, frameTime)}
timeToPing = 1.4f
}
}
20 changes: 10 additions & 10 deletions core/src/main/scala/my/game/pkg/client/actor/ClientActor.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package my.game.pkg.client.actor

import akka.actor.Actor

import my.game.pkg.Distributedlibgdx2dgame
import my.game.pkg.screen.MainGameScreen
import my.game.pkg.entity.RemotePlayer
import my.game.pkg.entity.{Player, RemotePlayer}
import my.game.pkg.client.dictionary.ClientDictionary._
import my.game.server.dictionary.ServerDictionary._

@@ -20,7 +19,8 @@ class ClientActor(val ipAddress:String, val port:String, val game:Distributedlib
*/
def receive = {
case Connect => remoteConnection ! Connect
case Connected(uuid) =>
case Connected(uuid, patch) =>
MainGameScreen.patch = patch
game.connectServerScreen.updateConnection(true)
game.gameUUID = Option(uuid)
context.become(connected)
@@ -34,9 +34,9 @@ class ClientActor(val ipAddress:String, val port:String, val game:Distributedlib
def connected:Receive = {
case Quit(uuid) => remoteConnection ! Quit(uuid)
case Move(uuid, map, direction) => remoteGameServer ! Move(uuid, map, direction)
case StandStill(uuid, map, x, y) => remoteGameServer ! StandStill(uuid, map, x, y)
case ChangeMap(uuid, mapFrom, mapTo, x, y) => remoteGameServer ! ChangeMap(uuid, mapFrom, mapTo, x, y)
case Alive(uuid, map, x, y, direction, state, frameTime) => remoteGameServer ! Alive(uuid, map, x, y, direction, state, frameTime)
case StandStill(uuid, patch, map, x, y) => remoteGameServer ! StandStill(uuid, patch, map, x, y)
case ChangeMap(uuid, patch, mapFrom, mapTo, x, y) => remoteGameServer ! ChangeMap(uuid, patch, mapFrom, mapTo, x, y)
case Alive(uuid, patch, map, x, y, direction, state, frameTime) => remoteGameServer ! Alive(uuid, patch, map, x, y, direction, state, frameTime)

case PlayerMove(uuid, direction) =>
if(!uuid.equals(game.gameUUID)){
@@ -51,7 +51,7 @@ class ClientActor(val ipAddress:String, val port:String, val game:Distributedlib
println("Move")
}

case PlayerStandStill(uuid, x, y) =>
case PlayerStandStill(uuid, patch, x, y) =>
if(!uuid.equals(game.gameUUID)){
var exist:Boolean = false
breakable{
@@ -64,12 +64,12 @@ class ClientActor(val ipAddress:String, val port:String, val game:Distributedlib
}
}
if(!exist){
MainGameScreen.remotePlayers += RemotePlayer(uuid, x, y)
MainGameScreen.remotePlayers += RemotePlayer(uuid, patch, x, y)
}
println("StandStill")
}

case Correction(uuid, x, y, direction, playerState, frameTime) =>
case Correction(uuid, patch, x, y, direction, playerState, frameTime) =>
var exist:Boolean = false
breakable{
for(remotePlayer <- MainGameScreen.remotePlayers){
@@ -81,7 +81,7 @@ class ClientActor(val ipAddress:String, val port:String, val game:Distributedlib
}
}
if(!exist){
MainGameScreen.remotePlayers += RemotePlayer(uuid, x, y)
MainGameScreen.remotePlayers += RemotePlayer(uuid, patch, x, y)
}
println("Correction")

Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ object ClientDictionary{
case object Connect
case class Quit(uuid:String)
case class Move(uuid:String, map:String, direction:Direction)
case class StandStill(uuid:String, map:String, x:Float, y:Float)
case class ChangeMap(uuid:String, mapFrom:String, mapTo:String, x:Float, y:Float)
case class Alive(uuid:String, map:String, x:Float, y:Float, direction:Direction, state:State, frameTime:Float)
case class StandStill(uuid:String, patch:Int, map:String, x:Float, y:Float)
case class ChangeMap(uuid:String, patch:Int, mapFrom:String, mapTo:String, x:Float, y:Float)
case class Alive(uuid:String, patch:Int, map:String, x:Float, y:Float, direction:Direction, state:State, frameTime:Float)
}
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ class PlayerController(val player:Player, val game:Distributedlibgdx2dgame) exte
game.client.foreach{client => client.move(Direction.DOWN)}
}
} else{
game.client.foreach{client => client.standStill(MainGameScreen.player.position.x, MainGameScreen.player.position.y)}
game.client.foreach{client => client.standStill(MainGameScreen.player.patch, MainGameScreen.player.position.x, MainGameScreen.player.position.y)}
}
}
true
16 changes: 8 additions & 8 deletions core/src/main/scala/my/game/pkg/entity/Player.scala
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import my.game.pkg.entity.utils.State
import my.game.pkg.entity.utils.State._
import my.game.pkg.assets.AssetsManager

class Player extends PlayerEntity {
class Player(patch:Int) extends PlayerEntity(patch) {


/**
@@ -32,26 +32,26 @@ class Player extends PlayerEntity {
case Direction.LEFT =>
nextPosition.x = position.x - velocity.x
nextPosition.y = position.y
currentFrame = PlayerEntity.walkLeftAnimation.getKeyFrame(frameTime)
currentFrame = walkLeftAnimation.getKeyFrame(frameTime)
case Direction.RIGHT =>
nextPosition.x = position.x + velocity.x
nextPosition.y = position.y
currentFrame = PlayerEntity.walkRightAnimation.getKeyFrame(frameTime)
currentFrame = walkRightAnimation.getKeyFrame(frameTime)
case Direction.UP =>
nextPosition.y = position.y + velocity.y
nextPosition.x = position.x
currentFrame = PlayerEntity.walkUpAnimation.getKeyFrame(frameTime)
currentFrame = walkUpAnimation.getKeyFrame(frameTime)
case Direction.DOWN =>
nextPosition.y = position.y - velocity.y
nextPosition.x = position.x
currentFrame = PlayerEntity.walkDownAnimation.getKeyFrame(frameTime)
currentFrame = walkDownAnimation.getKeyFrame(frameTime)
case _ =>
}
}
velocity.scl(1 / delta)
setBoundingSize(0f, 0.5f)
game.client match{
case Some(x) => x.update(delta, MainGameScreen.mapMgr.currentMapName, position.x, position.y, currentDirection, currentState:State, frameTime)
case Some(x) => x.update(delta, patch, MainGameScreen.mapMgr.currentMapName, position.x, position.y, currentDirection, currentState:State, frameTime)
case None =>
}
}
@@ -86,7 +86,7 @@ class Player extends PlayerEntity {
* Dispose of asset when not needed
*/
def dispose(){
AssetsManager.unloadAsset(PlayerEntity.defaultSpritePatch)
AssetsManager.unloadAsset(defaultSpritePatch)
}
}

@@ -96,7 +96,7 @@ object Player{
* Apply method for creating Player
* @return Player New instance of Player
*/
def apply():Player = new Player()
def apply(patch:Int):Player = new Player(patch)

private val TAG:String = Player.getClass().getSimpleName()
}
71 changes: 42 additions & 29 deletions core/src/main/scala/my/game/pkg/entity/PlayerEntity.scala
Original file line number Diff line number Diff line change
@@ -10,15 +10,54 @@ import my.game.pkg.assets.AssetsManager
import my.game.pkg.entity.utils.Direction
import my.game.pkg.map.MapManager

abstract class PlayerEntity extends Entity{
abstract class PlayerEntity(val patch:Int) extends Entity{

val velocity = new Vector2(10f, 10f)
var previousDirection = Direction.UP
val nextPosition = new Vector2()
val boundingBox = new Rectangle()
var currentFrame:TextureRegion = PlayerEntity.walkDownFrames.get(0)

val defaultSpritePatch:String = setupPatch(patch)

AssetsManager.loadTextureAsset(defaultSpritePatch)
val texture:Option[Texture] = AssetsManager.getTextureAsset(defaultSpritePatch)
val walkDownFrames = new Array[TextureRegion](4)
val walkLeftFrames = new Array[TextureRegion](4)
val walkRightFrames = new Array[TextureRegion](4)
val walkUpFrames = new Array[TextureRegion](4)
texture match{
case Some(tex) =>
for(x <- 0 to 3; val textureFrames:scala.Array[scala.Array[TextureRegion]] = TextureRegion.split(tex, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT)){
for(y <- 0 to 3){
x match{
case 0 => walkDownFrames.insert(y, textureFrames(x)(y))
case 1 => walkLeftFrames.insert(y, textureFrames(x)(y))
case 2 => walkRightFrames.insert(y, textureFrames(x)(y))
case 3 => walkUpFrames.insert(y, textureFrames(x)(y))
}
}
}
case None =>
}
val walkDownAnimation = new Animation[TextureRegion](0.25f, walkDownFrames, Animation.PlayMode.LOOP)
val walkLeftAnimation = new Animation[TextureRegion](0.25f, walkLeftFrames, Animation.PlayMode.LOOP)
val walkRightAnimation = new Animation[TextureRegion](0.25f, walkRightFrames, Animation.PlayMode.LOOP)
val walkUpAnimation = new Animation[TextureRegion](0.25f, walkUpFrames, Animation.PlayMode.LOOP)

var currentFrame:TextureRegion = walkDownFrames.get(0)
val frameSprite = new Sprite(currentFrame.getTexture(), 0, 0, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT)


//set player's patch according to patch:Int
def setupPatch(patch:Int): String ={
patch match{
case 0 => return Entity.spritePatchWarrior
case 1 => return Entity.spritePatchRogue
case 2 => return Entity.spritePatchMage
case 3 => return Entity.spritePatchPaladin
case 4 => return Entity.spritePatchEngineer
}
}

/**
* Set bounding box size for the player
* @param widthReduce:Float Width reduced in percentage
@@ -53,30 +92,4 @@ object PlayerEntity{

private val TAG:String = PlayerEntity.getClass().getSimpleName()

val defaultSpritePatch:String = Entity.spritePatchWarrior

AssetsManager.loadTextureAsset(PlayerEntity.defaultSpritePatch)
val texture:Option[Texture] = AssetsManager.getTextureAsset(PlayerEntity.defaultSpritePatch)
val walkDownFrames = new Array[TextureRegion](4)
val walkLeftFrames = new Array[TextureRegion](4)
val walkRightFrames = new Array[TextureRegion](4)
val walkUpFrames = new Array[TextureRegion](4)
texture match{
case Some(tex) =>
for(x <- 0 to 3; val textureFrames:scala.Array[scala.Array[TextureRegion]] = TextureRegion.split(tex, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT)){
for(y <- 0 to 3){
x match{
case 0 => walkDownFrames.insert(y, textureFrames(x)(y))
case 1 => walkLeftFrames.insert(y, textureFrames(x)(y))
case 2 => walkRightFrames.insert(y, textureFrames(x)(y))
case 3 => walkUpFrames.insert(y, textureFrames(x)(y))
}
}
}
case None =>
}
val walkDownAnimation = new Animation[TextureRegion](0.25f, walkDownFrames, Animation.PlayMode.LOOP)
val walkLeftAnimation = new Animation[TextureRegion](0.25f, walkLeftFrames, Animation.PlayMode.LOOP)
val walkRightAnimation = new Animation[TextureRegion](0.25f, walkRightFrames, Animation.PlayMode.LOOP)
val walkUpAnimation = new Animation[TextureRegion](0.25f, walkUpFrames, Animation.PlayMode.LOOP)
}
12 changes: 6 additions & 6 deletions core/src/main/scala/my/game/pkg/entity/RemotePlayer.scala
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import my.game.pkg.entity.utils.Direction._
import my.game.pkg.entity.utils.State
import my.game.pkg.entity.utils.State._

class RemotePlayer(val uuid:String, x:Float, y:Float) extends PlayerEntity {
class RemotePlayer(val uuid:String, patch:Int, x:Float, y:Float) extends PlayerEntity(patch) {

position.x = x
position.y = y
@@ -23,19 +23,19 @@ class RemotePlayer(val uuid:String, x:Float, y:Float) extends PlayerEntity {
case Direction.LEFT =>
nextPosition.x = position.x - velocity.x
nextPosition.y = position.y
currentFrame = PlayerEntity.walkLeftAnimation.getKeyFrame(frameTime)
currentFrame = walkLeftAnimation.getKeyFrame(frameTime)
case Direction.RIGHT =>
nextPosition.x = position.x + velocity.x
nextPosition.y = position.y
currentFrame = PlayerEntity.walkRightAnimation.getKeyFrame(frameTime)
currentFrame = walkRightAnimation.getKeyFrame(frameTime)
case Direction.UP =>
nextPosition.y = position.y + velocity.y
nextPosition.x = position.x
currentFrame = PlayerEntity.walkUpAnimation.getKeyFrame(frameTime)
currentFrame = walkUpAnimation.getKeyFrame(frameTime)
case Direction.DOWN =>
nextPosition.y = position.y - velocity.y
nextPosition.x = position.x
currentFrame = PlayerEntity.walkDownAnimation.getKeyFrame(frameTime)
currentFrame = walkDownAnimation.getKeyFrame(frameTime)
case _ =>
}
}
@@ -88,7 +88,7 @@ object RemotePlayer{
* @param x:Float Y position of the player
* @param y:Float X position of the player
*/
def apply(uuid:String, x:Float, y:Float):RemotePlayer = new RemotePlayer(uuid, x, y)
def apply(uuid:String, patch:Int, x:Float, y:Float):RemotePlayer = new RemotePlayer(uuid, patch, x, y)

private val TAG:String = RemotePlayer.getClass().getSimpleName()
}
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ class ConnectServerScreen(val game:Distributedlibgdx2dgame) extends Screen{
val table = new Table()

val ipLabel = new Label("IP Address:", AssetsManager.STATUSUI_SKIN)
val ipTextField = new TextField("127.0.0.1", AssetsManager.STATUSUI_SKIN)
val ipTextField = new TextField("210.186.240.171", AssetsManager.STATUSUI_SKIN)
val portLabel = new Label("Port:", AssetsManager.STATUSUI_SKIN)
val portTextField = new TextField("8080", AssetsManager.STATUSUI_SKIN)
val connectButton = new TextButton("Connect", AssetsManager.STATUSUI_SKIN)
27 changes: 16 additions & 11 deletions core/src/main/scala/my/game/pkg/screen/MainGameScreen.scala
Original file line number Diff line number Diff line change
@@ -3,12 +3,10 @@ package my.game.pkg.screen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.{GL20, OrthographicCamera}
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.{BitmapFont, Sprite, SpriteBatch, TextureRegion}
import com.badlogic.gdx.maps.objects.RectangleMapObject
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.graphics.g2d.{BitmapFont, SpriteBatch}

import my.game.pkg.Distributedlibgdx2dgame
import my.game.pkg.map.MapManager
import my.game.pkg.controller.PlayerController
@@ -21,10 +19,10 @@ import scala.collection.mutable.ListBuffer

class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{

val controller = PlayerController(MainGameScreen.player, game)
//var controller : PlayerController
var currentPlayerFrame:TextureRegion = null
var currentPlayerSprite = MainGameScreen.player.frameSprite
val mapRenderer = new OrthogonalTiledMapRenderer(MainGameScreen.mapMgr.getCurrentMap(), MapManager.UNIT_SCALE)
var currentPlayerSprite : Sprite = null
var mapRenderer = new OrthogonalTiledMapRenderer(MainGameScreen.mapMgr.getCurrentMap(), MapManager.UNIT_SCALE)
val camera = new OrthographicCamera()
val font = new BitmapFont()
val spriteBatch = new SpriteBatch()
@@ -33,6 +31,11 @@ class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{
* Execute when no screen is showed
*/
override def show{

MainGameScreen.player = Player(MainGameScreen.patch)
MainGameScreen.controller = PlayerController(MainGameScreen.player, game)
currentPlayerSprite = MainGameScreen.player.frameSprite

MainGameScreen.VIEWPORT.setupViewport(15, 15, Gdx.graphics.getWidth(), Gdx.graphics.getHeight())

camera.setToOrtho(false, MainGameScreen.VIEWPORT.viewportWidth, MainGameScreen.VIEWPORT.viewportHeight)
@@ -43,7 +46,7 @@ class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{
MainGameScreen.player.init(MainGameScreen.mapMgr.getPlayerStartUnitScaled)
MainGameScreen.player.move()
MainGameScreen.npc.init()
Gdx.input.setInputProcessor(controller)
Gdx.input.setInputProcessor(MainGameScreen.controller)
}

/**
@@ -59,7 +62,7 @@ class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{
Gdx.gl.glClearColor(0, 0, 0, 1)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

controller.update(delta)
MainGameScreen.controller.update(delta)
MainGameScreen.npc.update(delta)
currentPlayerFrame = MainGameScreen.player.currentFrame

@@ -122,7 +125,7 @@ class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{
*/
override def dispose{
MainGameScreen.player.dispose()
controller.dispose()
MainGameScreen.controller.dispose()
Gdx.input.setInputProcessor(null)
mapRenderer.dispose()
font.dispose()
@@ -181,7 +184,7 @@ class MainGameScreen(val game:Distributedlibgdx2dgame) extends Screen{
mapRenderer.setMap(MainGameScreen.mapMgr.currentMap)
Gdx.app.debug(MainGameScreen.TAG, "Portal Activated")
game.client match{
case Some(x) => x.changeMap(MainGameScreen.mapMgr.previousMapName, MainGameScreen.mapMgr.currentMapName,
case Some(x) => x.changeMap(MainGameScreen.player.patch, MainGameScreen.mapMgr.previousMapName, MainGameScreen.mapMgr.currentMapName,
MainGameScreen.player.position.x, MainGameScreen.player.position.y)
case None =>
}
@@ -209,7 +212,9 @@ object MainGameScreen {
var mapMgr:MapManager = MapManager()

var npc = MapNPCs(MapManager.TOWN)
val player = Player()
var patch:Int = 0
var controller : PlayerController = null
var player : Player = null
val remotePlayers = new ListBuffer[RemotePlayer]()

private object VIEWPORT{
Original file line number Diff line number Diff line change
@@ -4,10 +4,17 @@ import my.game.pkg.entity.utils.Direction._
import my.game.pkg.entity.utils.State._

object ServerDictionary{
case class Connected(uuid:String)
case class Connected(uuid:String, patch:Int)
case class PlayerMove(uuid:String, direction:Direction)
case class PlayerStandStill(uuid:String, x:Float, y:Float)
case class PlayerStandStill(uuid:String, patch:Int, x:Float, y:Float)
case class KillPlayer(uuid:String)
case class NotAlive(uuid:String, map:String)
case class Correction(uuid:String, x:Float, y:Float, direction:Direction, state:State, frameTime:Float)
case class Correction(uuid:String, patch:Int, x:Float, y:Float, direction:Direction, state:State, frameTime:Float)

//SpritePatch constants for players
val warrior = 0
val rogue = 1
val mage = 2
val paladin = 3
val engineer = 4
}