Skip to content

Commit

Permalink
Added a Python script to generate tiles, map data and source code fro…
Browse files Browse the repository at this point in the history
…m a tile sheet.

This generated code can be used with the Tiled library in an Android activity then.
  • Loading branch information
saifkhichi96 committed Jan 19, 2021
1 parent 18ea770 commit 170e999
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 86 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
compileSdkVersion 29

defaultConfig {
applicationId "co.aspirasoft.android.demo"
applicationId "dev.aspirasoft.apis.tiled.demo"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
Expand Down
173 changes: 88 additions & 85 deletions demo/src/main/java/dev/aspirasoft/apis/tiled/demo/MarioDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,100 +11,103 @@ class MarioDemo : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo)
loadMapFromAssets("mario.txt")
}

var reader: BufferedReader? = null
try {
reader = BufferedReader(InputStreamReader(assets.open("mario.txt"), "UTF-8"));

// do reading, usually loop until end of file reading
var map = ""
/**
* Loads map data from a file in assets folder.
*
* @param filename Name of the file in assets folder.
* @throws Exception If the given file could not be opened.
*/
@Throws(Exception::class)
private fun loadMapFromAssets(filename: String) {
val reader = BufferedReader(InputStreamReader(assets.open(filename), "UTF-8"))
reader.use {
var data = ""
var line = reader.readLine()
while (line != null) {
map += line + "\n"
data += line + "\n"
line = reader.readLine()
}

val mapData = arrayOf(map)
arrayOf(R.id.tiledView).withIndex().forEach { (index, id) ->
val tiledView = findViewById<TiledView>(id)
createLookupMap().forEach { (id, image) -> tiledView.addImage(id, image) }
tiledView.post {
tiledView.setData(mapData[index])
}
tiledView.setOnTileClicked { c: Int, r: Int ->
val char = tiledView[c, r]
val fruits = listOf("e", "n", "o")
if (char in fruits) {
tiledView[c, r] = ""
}
}
}
} catch (ex: Exception) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (ex: Exception) {
//log the exception
}
}
onMapDataReady(data)
}
}

/**
* Receives the map data to finish tilemap setup.
*
* @param data The complete tilemap, encoded in a string.
*/
private fun onMapDataReady(data: String) {
val tiledView = findViewById<TiledView>(R.id.tiledView)
createLookupTable(tiledView)
tiledView.post { tiledView.setData(data) }
tiledView.setOnTileClicked { column: Int, row: Int ->
// TODO: Handle clicks on tiles here
}
}

private fun createLookupMap(): HashMap<String, Int> {
val map = HashMap<String, Int>()
map["a"] = R.drawable.t_mario_a
map["b"] = R.drawable.t_mario_b
map["c"] = R.drawable.t_mario_c
map["d"] = R.drawable.t_mario_d
map["e"] = R.drawable.t_mario_e
map["f"] = R.drawable.t_mario_f
map["g"] = R.drawable.t_mario_g
map["h"] = R.drawable.t_mario_h
map["i"] = R.drawable.t_mario_i
map["j"] = R.drawable.t_mario_j
map["k"] = R.drawable.t_mario_k
map["l"] = R.drawable.t_mario_l
map["m"] = R.drawable.t_mario_m
map["n"] = R.drawable.t_mario_n
map["o"] = R.drawable.t_mario_o
map["p"] = R.drawable.t_mario_p
map["q"] = R.drawable.t_mario_q
map["r"] = R.drawable.t_mario_r
map["s"] = R.drawable.t_mario_s
map["t"] = R.drawable.t_mario_t
map["u"] = R.drawable.t_mario_u
map["v"] = R.drawable.t_mario_v
map["w"] = R.drawable.t_mario_w
map["x"] = R.drawable.t_mario_x
map["y"] = R.drawable.t_mario_y
map["z"] = R.drawable.t_mario_z
map["aa"] = R.drawable.t_mario_aa
map["bb"] = R.drawable.t_mario_bb
map["cc"] = R.drawable.t_mario_cc
map["dd"] = R.drawable.t_mario_dd
map["ee"] = R.drawable.t_mario_ee
map["ff"] = R.drawable.t_mario_ff
map["gg"] = R.drawable.t_mario_gg
map["hh"] = R.drawable.t_mario_hh
map["ii"] = R.drawable.t_mario_ii
map["jj"] = R.drawable.t_mario_jj
map["kk"] = R.drawable.t_mario_kk
map["ll"] = R.drawable.t_mario_ll
map["mm"] = R.drawable.t_mario_mm
map["nn"] = R.drawable.t_mario_nn
map["oo"] = R.drawable.t_mario_oo
map["pp"] = R.drawable.t_mario_pp
map["qq"] = R.drawable.t_mario_qq
map["rr"] = R.drawable.t_mario_rr
map["ss"] = R.drawable.t_mario_ss
map["tt"] = R.drawable.t_mario_tt
map["uu"] = R.drawable.t_mario_uu
map["vv"] = R.drawable.t_mario_vv
map["ww"] = R.drawable.t_mario_ww
map["xx"] = R.drawable.t_mario_xx
return map
/**
* Creates a lookup table for the tilemap.
*
* A lookup table maps keys to a drawable resources which should be
* drawn in place of that key.
*
* @param tilemap The tilemap view for which to create the mapping.
*/
private fun createLookupTable(tilemap: TiledView) {
tilemap.addImage("a", R.drawable.t_mario_a)
tilemap.addImage("b", R.drawable.t_mario_b)
tilemap.addImage("c", R.drawable.t_mario_c)
tilemap.addImage("d", R.drawable.t_mario_d)
tilemap.addImage("e", R.drawable.t_mario_e)
tilemap.addImage("f", R.drawable.t_mario_f)
tilemap.addImage("g", R.drawable.t_mario_g)
tilemap.addImage("h", R.drawable.t_mario_h)
tilemap.addImage("i", R.drawable.t_mario_i)
tilemap.addImage("j", R.drawable.t_mario_j)
tilemap.addImage("k", R.drawable.t_mario_k)
tilemap.addImage("l", R.drawable.t_mario_l)
tilemap.addImage("m", R.drawable.t_mario_m)
tilemap.addImage("n", R.drawable.t_mario_n)
tilemap.addImage("o", R.drawable.t_mario_o)
tilemap.addImage("p", R.drawable.t_mario_p)
tilemap.addImage("q", R.drawable.t_mario_q)
tilemap.addImage("r", R.drawable.t_mario_r)
tilemap.addImage("s", R.drawable.t_mario_s)
tilemap.addImage("t", R.drawable.t_mario_t)
tilemap.addImage("u", R.drawable.t_mario_u)
tilemap.addImage("v", R.drawable.t_mario_v)
tilemap.addImage("w", R.drawable.t_mario_w)
tilemap.addImage("x", R.drawable.t_mario_x)
tilemap.addImage("y", R.drawable.t_mario_y)
tilemap.addImage("z", R.drawable.t_mario_z)
tilemap.addImage("aa", R.drawable.t_mario_aa)
tilemap.addImage("bb", R.drawable.t_mario_bb)
tilemap.addImage("cc", R.drawable.t_mario_cc)
tilemap.addImage("dd", R.drawable.t_mario_dd)
tilemap.addImage("ee", R.drawable.t_mario_ee)
tilemap.addImage("ff", R.drawable.t_mario_ff)
tilemap.addImage("gg", R.drawable.t_mario_gg)
tilemap.addImage("hh", R.drawable.t_mario_hh)
tilemap.addImage("ii", R.drawable.t_mario_ii)
tilemap.addImage("jj", R.drawable.t_mario_jj)
tilemap.addImage("kk", R.drawable.t_mario_kk)
tilemap.addImage("ll", R.drawable.t_mario_ll)
tilemap.addImage("mm", R.drawable.t_mario_mm)
tilemap.addImage("nn", R.drawable.t_mario_nn)
tilemap.addImage("oo", R.drawable.t_mario_oo)
tilemap.addImage("pp", R.drawable.t_mario_pp)
tilemap.addImage("qq", R.drawable.t_mario_qq)
tilemap.addImage("rr", R.drawable.t_mario_rr)
tilemap.addImage("ss", R.drawable.t_mario_ss)
tilemap.addImage("tt", R.drawable.t_mario_tt)
tilemap.addImage("uu", R.drawable.t_mario_uu)
tilemap.addImage("vv", R.drawable.t_mario_vv)
tilemap.addImage("ww", R.drawable.t_mario_ww)
tilemap.addImage("xx", R.drawable.t_mario_xx)
}

}
143 changes: 143 additions & 0 deletions utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Datasets
data/
2 changes: 2 additions & 0 deletions utils/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.19.5
opencv-python==4.5.1.48
Loading

0 comments on commit 170e999

Please sign in to comment.