Skip to content

Instantly share code, notes, and snippets.

@abueide
Created February 3, 2020 17:21
Show Gist options
  • Save abueide/3da5cb6da49c6a54ae023c5b5a997027 to your computer and use it in GitHub Desktop.
Save abueide/3da5cb6da49c6a54ae023c5b5a997027 to your computer and use it in GitHub Desktop.
package com.abyssalrealms
import com.abyssalrealms.ecs.components.MovementComponent
import com.abyssalrealms.ecs.components.PlayerComponent
import com.abyssalrealms.ecs.components.PositionComponent
import com.abyssalrealms.ecs.components.RenderComponent
import com.abyssalrealms.ecs.systems.InputSystem
import com.abyssalrealms.ecs.systems.MovementSystem
import com.abyssalrealms.ecs.systems.RenderSystem
import com.artemis.World
import com.artemis.WorldConfigurationBuilder
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import com.badlogic.gdx.math.Vector2
/** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */
class Client : ApplicationAdapter() {
lateinit var batch: SpriteBatch
lateinit var world: World
lateinit var cam: OrthographicCamera
lateinit var map: TiledMap
lateinit var mapRenderer: OrthogonalTiledMapRenderer
var playerId: Int = 0
var viewPortSize = 20f
override fun create() {
batch = SpriteBatch();
map = TmxMapLoader().load("tilemaps/demo/oryx_demo.tmx")
mapRenderer = OrthogonalTiledMapRenderer(map, 1/24f);
val w = Gdx.graphics.width
val h = Gdx.graphics.height
cam = OrthographicCamera(viewPortSize, viewPortSize * (h / w))
cam.setToOrtho(false, viewPortSize, viewPortSize)
cam.position.set(0f, 0f, 0f)
// cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0f)
cam.update()
val input = InputSystem()
Gdx.input.inputProcessor = input
// Entity Initialization
val setup = WorldConfigurationBuilder().with(
input, MovementSystem(), RenderSystem(batch)
).build()
world = World(setup)
playerId = world.create()
world.edit(playerId).create(PositionComponent::class.java).position = Vector2(0f, 0f)
world.edit(playerId).create(RenderComponent::class.java).image = Texture("oryxlabs/Sliced/creatures_24x24/oryx_16bit_fantasy_creatures_31.png")
world.edit(playerId).create(MovementComponent::class.java).also { it.maxSpeed = 5f }.accSpeed = 100f
world.edit(playerId).create(PlayerComponent::class.java).local = true
}
override fun render() {
val position = world.getEntity(playerId).getComponent<PositionComponent>(PositionComponent::class.java).position
cam.position.set(position.x, position.y, 0f)
cam.update()
batch.projectionMatrix = cam.combined
mapRenderer.setView(cam)
Gdx.gl.glClearColor(0f, 0f, 1f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
mapRenderer.render()
batch.begin()
world.process()
batch.end()
}
override fun resize(width: Int, height: Int) {
cam.viewportWidth = viewPortSize
cam.viewportHeight = viewPortSize * height/width;
cam.update();
}
override fun dispose() {
batch.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment