├── .gitattributes ├── .gitignore ├── .gitmodules ├── Makefile ├── README.adoc ├── boing ├── README.md ├── images │ ├── ball.png │ ├── bat00.png │ ├── bat01.png │ ├── bat02.png │ ├── bat10.png │ ├── bat11.png │ ├── bat12.png │ ├── blank.png │ ├── digit00.png │ ├── digit01.png │ ├── digit02.png │ ├── digit03.png │ ├── digit04.png │ ├── digit05.png │ ├── digit06.png │ ├── digit07.png │ ├── digit08.png │ ├── digit09.png │ ├── digit10.png │ ├── digit11.png │ ├── digit12.png │ ├── digit13.png │ ├── digit14.png │ ├── digit15.png │ ├── digit16.png │ ├── digit17.png │ ├── digit18.png │ ├── digit19.png │ ├── digit20.png │ ├── digit21.png │ ├── digit22.png │ ├── digit23.png │ ├── digit24.png │ ├── digit25.png │ ├── digit26.png │ ├── digit27.png │ ├── digit28.png │ ├── digit29.png │ ├── effect0.png │ ├── effect1.png │ ├── impact0.png │ ├── impact1.png │ ├── impact2.png │ ├── impact3.png │ ├── impact4.png │ ├── menu0.png │ ├── menu1.png │ ├── over.png │ └── table.png ├── main.wren ├── music │ └── theme.ogg ├── screenshot.png ├── sounds │ ├── bounce0.ogg │ ├── bounce1.ogg │ ├── bounce2.ogg │ ├── bounce3.ogg │ ├── bounce4.ogg │ ├── bounce_synth0.ogg │ ├── down.ogg │ ├── hit0.ogg │ ├── hit1.ogg │ ├── hit2.ogg │ ├── hit3.ogg │ ├── hit4.ogg │ ├── hit_fast0.ogg │ ├── hit_medium0.ogg │ ├── hit_slow0.ogg │ ├── hit_synth0.ogg │ ├── hit_veryfast0.ogg │ ├── score_goal0.ogg │ └── up.ogg └── zero.wren ├── fps ├── DUMMY1.png ├── DUMMY2.png ├── DUMMY3.png ├── DUMMY4.png ├── DUMMY5.png ├── DUMMY6.png ├── DUMMY7.png ├── DUMMY8.png ├── ceil.png ├── column.png ├── column2.png ├── context.wren ├── door.png ├── door.wren ├── floor.png ├── keys.wren ├── main.wren ├── map.wren ├── renderer.wren ├── sprite.wren ├── texture.wren ├── wall1.png ├── wall2.png ├── wall3.png └── wall4.png ├── platformer ├── keys.wren └── main.wren ├── roguelike ├── action.wren ├── actor.wren ├── dir.wren ├── events.wren ├── item.wren ├── keys.wren ├── main.wren ├── map.wren ├── model.wren ├── sprite.wren └── view.wren └── tileeditor ├── bg.csv ├── cute.png ├── keys.wren ├── level.map ├── level.map.bak ├── main.wren ├── map.csv ├── map.csv.bak ├── model.wren └── tileset.png /.gitattributes: -------------------------------------------------------------------------------- 1 | $ cat .gitattributes 2 | * text=auto 3 | *.wren linguist-language=JavaScript -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | DOME-out.log 3 | *.gif 4 | **/screenshot.png 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "breakout"] 2 | path = breakout 3 | url = https://github.com/NinjasCL/breakout 4 | [submodule "snake"] 5 | path = snake 6 | url = https://github.com/NinjasCL/snake 7 | [submodule "munstajump"] 8 | path = munstajump 9 | url = https://github.com/NinjasCL/munstajump 10 | [submodule "snake2"] 11 | path = snake2 12 | url = https://github.com/joshuacross/snake 13 | [submodule "pongflow"] 14 | path = pongflow 15 | url = https://github.com/ruby0x1/dome-pong-flow 16 | [submodule "gpracer"] 17 | path = gpracer 18 | url = https://github.com/DH001/gpracer 19 | [submodule "cleanthatcastle"] 20 | path = cleanthatcastle 21 | url = https://github.com/chrislewisdev/domejam 22 | [submodule "breathe"] 23 | path = breathe 24 | url = https://github.com/avivbeeri/breathe 25 | [submodule "rhythmdrive"] 26 | path = rhythmdrive 27 | url = https://github.com/avivbeeri/RhythmDrive 28 | [submodule "popup"] 29 | path = popup 30 | url = https://github.com/whmsft/popup 31 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: install 2 | 3 | i install: 4 | git submodule init 5 | git submodule update -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | :ext-relative: 2 | :toc: macro 3 | :toclevels: 4 4 | 5 | # DOME Examples 6 | 7 | Examples made with the https://domeengine.com[DOME engine] 8 | 9 | Make sure DOME is installed, then you can point it at each example directory. 10 | 11 | Each example is licensed independently. Please abide by the terms of each example's license. 12 | 13 | toc::[] 14 | 15 | ## Installation 16 | 17 | Some examples use https://git-scm.com/docs/git-submodule[Git Submodules]. Git doesn't actually pull the submodules immediately, so you need to enter the right commands to make that happen. 18 | 19 | ```sh 20 | $ git submodule init 21 | $ git submodule update 22 | ``` 23 | 24 | ## Jams 25 | 26 | A Jam is an opportunity to build games with DOME, the comfortable framework for making games. It allows you to make cross-platform 2D games (for Windows, Mac and Linux) with ease. 27 | 28 | It's a time to learn the https://wren.io[Wren programming language], leave the nest and make cute, cosy and cool games! 29 | 30 | ### Rules 31 | - You have about 10 days to make your game so that you have time to learn Wren and the DOME APIs. 32 | - Your entry must use DOME. 33 | - The entry must be considered "safe-for-work". 34 | - Assets (images, music, text, code) must be your own, or open-licensed. 35 | - Your final game must be open-sourced. 36 | - Have fun! 37 | 38 | ### https://itch.io/jam/domejam[DOMEJam March 2020] 39 | The optional theme was: ~FLOW~ 40 | 41 | ## Examples 42 | 43 | ### link:boing{ext-relative}[Boing!] 44 | Pong clone from https://wireframe.raspberrypi.org/books/code-the-classics1["Code The Classics Volume 1"] 45 | 46 | ### link:platformer{ext-relative}[Platformer] 47 | A simple platformer engine, based on https://mattmakesgames.tumblr.com/post/127890619821/towerfall-physics[this article] about the physics of Towerfall. 48 | 49 | ### link:tileeditor{ext-relative}[TileMap Editor] 50 | An in-engine tilemap editor, based on the Platformer example. 51 | 52 | ### link:roguelike{ext-relative}[Roguelike] 53 | The bare-basic framework for a top-down roguelike, based on the turn-based game loop discussed https://journal.stuffwithstuff.com/2014/07/15/a-turn-based-game-loop/[here]. 54 | 55 | ### https://github.com/NinjasCL/snake[Snake] 56 | A simple snake game clone. 57 | 58 | ### https://github.com/joshuacross/snake[Snake 2] 59 | A more elaborate snake game clone. 60 | 61 | ### https://github.com/NinjasCL/breakout[Breakout] 62 | A simple breakout game clone. 63 | 64 | ### https://github.com/NinjasCL/munstajump[Munstajump] 65 | A Simple Monster Jumping Game. 66 | 67 | ### https://github.com/ruby0x1/dome-pong-flow[Pong Flow] 68 | To win, instead of making it failure based, success is used. If you hit the ball, you gain a point, and the first to a certain number wins. 69 | 70 | ### https://github.com/DH001/gpracer[DOME Racer] 71 | A simple racing game using DOME engine 72 | 73 | ### https://github.com/chrislewisdev/domejam[Clean that Castle!] 74 | The castle barracks is a mess! Swords, bows, and shields everywhere... I guess it's up to you to... Clean That Castle! 75 | 76 | ### https://github.com/avivbeeri/breathe[Breathe] 77 | A simple application for helping to pace your breathing. 78 | Hold the spacebar as you inhale. Watch the circle grow, then release it as you exhale. 79 | Relax. It will be okay. 80 | 81 | ### https://github.com/avivbeeri/RhythmDrive[Rhythm Drive] 82 | In the far-flung future, faster-than-light travel has been discovered, through the traversal of "RhythmSpace". Engage your space-ship's "rhythmdrive" and take to the stars! 83 | 84 | ### https://github.com/whmsft/popup[PopUp] 85 | A simple pop-up game where your work is to close as many popups you can! 86 | -------------------------------------------------------------------------------- /boing/README.md: -------------------------------------------------------------------------------- 1 | Assets (Sounds, Music and Images) are licensed under: 2 | 3 | Creative Commons 4 | Attribution Non-Commercial ShareAlike 3.0 Unported 5 | 6 | https://creativecommons.org/licenses/by-nc-sa/3.0/ 7 | -------------------------------------------------------------------------------- /boing/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/ball.png -------------------------------------------------------------------------------- /boing/images/bat00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat00.png -------------------------------------------------------------------------------- /boing/images/bat01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat01.png -------------------------------------------------------------------------------- /boing/images/bat02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat02.png -------------------------------------------------------------------------------- /boing/images/bat10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat10.png -------------------------------------------------------------------------------- /boing/images/bat11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat11.png -------------------------------------------------------------------------------- /boing/images/bat12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/bat12.png -------------------------------------------------------------------------------- /boing/images/blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/blank.png -------------------------------------------------------------------------------- /boing/images/digit00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit00.png -------------------------------------------------------------------------------- /boing/images/digit01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit01.png -------------------------------------------------------------------------------- /boing/images/digit02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit02.png -------------------------------------------------------------------------------- /boing/images/digit03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit03.png -------------------------------------------------------------------------------- /boing/images/digit04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit04.png -------------------------------------------------------------------------------- /boing/images/digit05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit05.png -------------------------------------------------------------------------------- /boing/images/digit06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit06.png -------------------------------------------------------------------------------- /boing/images/digit07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit07.png -------------------------------------------------------------------------------- /boing/images/digit08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit08.png -------------------------------------------------------------------------------- /boing/images/digit09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit09.png -------------------------------------------------------------------------------- /boing/images/digit10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit10.png -------------------------------------------------------------------------------- /boing/images/digit11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit11.png -------------------------------------------------------------------------------- /boing/images/digit12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit12.png -------------------------------------------------------------------------------- /boing/images/digit13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit13.png -------------------------------------------------------------------------------- /boing/images/digit14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit14.png -------------------------------------------------------------------------------- /boing/images/digit15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit15.png -------------------------------------------------------------------------------- /boing/images/digit16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit16.png -------------------------------------------------------------------------------- /boing/images/digit17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit17.png -------------------------------------------------------------------------------- /boing/images/digit18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit18.png -------------------------------------------------------------------------------- /boing/images/digit19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit19.png -------------------------------------------------------------------------------- /boing/images/digit20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit20.png -------------------------------------------------------------------------------- /boing/images/digit21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit21.png -------------------------------------------------------------------------------- /boing/images/digit22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit22.png -------------------------------------------------------------------------------- /boing/images/digit23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit23.png -------------------------------------------------------------------------------- /boing/images/digit24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit24.png -------------------------------------------------------------------------------- /boing/images/digit25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit25.png -------------------------------------------------------------------------------- /boing/images/digit26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit26.png -------------------------------------------------------------------------------- /boing/images/digit27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit27.png -------------------------------------------------------------------------------- /boing/images/digit28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit28.png -------------------------------------------------------------------------------- /boing/images/digit29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/digit29.png -------------------------------------------------------------------------------- /boing/images/effect0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/effect0.png -------------------------------------------------------------------------------- /boing/images/effect1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/effect1.png -------------------------------------------------------------------------------- /boing/images/impact0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/impact0.png -------------------------------------------------------------------------------- /boing/images/impact1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/impact1.png -------------------------------------------------------------------------------- /boing/images/impact2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/impact2.png -------------------------------------------------------------------------------- /boing/images/impact3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/impact3.png -------------------------------------------------------------------------------- /boing/images/impact4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/impact4.png -------------------------------------------------------------------------------- /boing/images/menu0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/menu0.png -------------------------------------------------------------------------------- /boing/images/menu1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/menu1.png -------------------------------------------------------------------------------- /boing/images/over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/over.png -------------------------------------------------------------------------------- /boing/images/table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/images/table.png -------------------------------------------------------------------------------- /boing/main.wren: -------------------------------------------------------------------------------- 1 | import "audio" for AudioEngine 2 | import "math" for M, Vec 3 | import "graphics" for Canvas, Color, ImageData 4 | import "input" for Keyboard 5 | import "dome" for Window, Process 6 | import "./zero" for Actor 7 | import "random" for Random 8 | var Generator = Random.new() 9 | 10 | var WIDTH = 800 11 | var HEIGHT = 480 12 | var TITLE = "Boing!" 13 | var HALF_W = WIDTH / 2 14 | var HALF_H = HEIGHT / 2 15 | var PLAYER_SPEED = 6 16 | var MAX_AI_SPEED = 6 17 | 18 | class Impact is Actor { 19 | construct new(pos) { 20 | super("blank", pos) 21 | _time = 0 22 | } 23 | 24 | time { _time } 25 | 26 | update() { 27 | image = "impact%((_time / 2).floor)" 28 | _time = _time + 1 29 | } 30 | } 31 | 32 | class Bat is Actor { 33 | construct new(game, player, isPlayer) { 34 | super("blank", Vec.new()) 35 | _game = game 36 | if (player == 0) { 37 | pos.x = 40 38 | } else { 39 | pos.x = 760 40 | } 41 | pos.y = HALF_H 42 | _player = player 43 | _score = 0 44 | _timer = 0 45 | _isPlayer = isPlayer || false 46 | } 47 | 48 | draw(alpha) { 49 | super.draw(alpha) 50 | // Canvas.rectfill(pos.x - 9, pos.y - 57, 18, 114, Color.green) 51 | Canvas.pset(pos.x, pos.y, Color.red) 52 | } 53 | 54 | ai_move() { 55 | var dX = M.abs(_game.ball.pos.x - pos.x) 56 | var tY1 = _game.ball.pos.y + _game.ai_offset 57 | var tY2 = HALF_H 58 | var weight = M.min(1, dX / HALF_W) 59 | var tY = M.lerp(tY1, weight, tY2) 60 | return M.mid(MAX_AI_SPEED, -MAX_AI_SPEED, (tY - pos.y)) 61 | } 62 | 63 | player1_move() { 64 | if (Keyboard.isKeyDown("up") || Keyboard.isKeyDown("a")) { 65 | return -PLAYER_SPEED 66 | } 67 | if (Keyboard.isKeyDown("down") || Keyboard.isKeyDown("z")) { 68 | return PLAYER_SPEED 69 | } 70 | return 0 71 | } 72 | 73 | player2_move() { 74 | if (Keyboard.isKeyDown("m")) { 75 | return PLAYER_SPEED 76 | } 77 | if (Keyboard.isKeyDown("k")) { 78 | return -PLAYER_SPEED 79 | } 80 | return 0 81 | } 82 | 83 | update() { 84 | _timer = _timer - 1 85 | var dY 86 | if (!_isPlayer) { 87 | dY = this.ai_move() 88 | } else { 89 | if (_player == 0) { 90 | dY = this.player1_move() 91 | } else { 92 | dY = this.player2_move() 93 | } 94 | } 95 | 96 | pos.y = M.mid(400, 80, pos.y + dY) 97 | var frame = 0 98 | if (_timer > 0) { 99 | if (_game.ball.out()) { 100 | frame = 2 101 | } else { 102 | frame = 1 103 | } 104 | } 105 | image = "bat%(_player)%(frame)" 106 | } 107 | 108 | timer=(v) { _timer = v } 109 | score=(v) { _score = v } 110 | timer { _timer } 111 | score { _score } 112 | } 113 | 114 | class Ball is Actor { 115 | construct new(game, direction) { 116 | super("ball", Vec.new(HALF_W, HALF_H)) 117 | _speed = 5 118 | _vel = Vec.new(direction, 0) 119 | _game = game 120 | } 121 | 122 | update() { 123 | for (i in 0..._speed) { 124 | var original = pos 125 | pos = pos + _vel 126 | 127 | if (M.abs(pos.x - HALF_W) >= 344 && M.abs(original.x - HALF_W) < 344) { 128 | var direction = 0 129 | var bat 130 | if (pos.x < HALF_W) { 131 | direction = 1 132 | bat = _game.bats[0] 133 | } else { 134 | direction = -1 135 | bat = _game.bats[1] 136 | } 137 | 138 | var dY = pos.y - bat.pos.y 139 | 140 | if (dY > -64 && dY < 64) { 141 | _vel.x = -_vel.x 142 | _vel.y = M.mid(-1, _vel.y + dY / 128, 1) 143 | 144 | _vel = _vel.unit 145 | _game.impact(Vec.new(pos.x - direction * 10, pos.y)) 146 | 147 | _speed = _speed + 1 148 | _game.ai_offset = Generator.int(-10, 11) 149 | 150 | bat.timer = 10 151 | 152 | var num = Generator.int(5) 153 | AudioEngine.play("hit%(num)") 154 | if (_speed <= 10) { 155 | AudioEngine.play("hit_slow") 156 | } else if (_speed <= 12) { 157 | AudioEngine.play("hit_medium") 158 | } else if (_speed <= 16) { 159 | AudioEngine.play("hit_fast") 160 | } else { 161 | AudioEngine.play("hit_veryfast") 162 | } 163 | } 164 | } 165 | if (M.abs(pos.y - HALF_H) > 220) { 166 | _vel.y = -_vel.y 167 | pos.y = pos.y + _vel.y 168 | _game.impact(pos) 169 | var num = Generator.int(5) 170 | AudioEngine.play("bounce%(num)") 171 | AudioEngine.play("bounce_synth") 172 | } 173 | } 174 | 175 | } 176 | 177 | out() { 178 | return pos.x < 0 || pos.x > WIDTH 179 | } 180 | } 181 | 182 | class GameState { 183 | 184 | construct init(playerCount) { 185 | _bats = [Bat.new(this, 0, playerCount >= 1), Bat.new(this, 1, playerCount >= 2)] 186 | 187 | _ball = Ball.new(this, -1) 188 | _impacts = [] 189 | _ai_offset = 0 190 | Window.resize(WIDTH, HEIGHT) 191 | Canvas.resize(WIDTH, HEIGHT) 192 | Window.title = TITLE 193 | 194 | _table = ImageData.loadFromFile("images/table.png") 195 | 196 | AudioEngine.load("score_goal", "sounds/score_goal0.ogg") 197 | AudioEngine.load("hit_slow", "sounds/hit_slow0.ogg") 198 | AudioEngine.load("hit_medium", "sounds/hit_medium0.ogg") 199 | AudioEngine.load("hit_fast", "sounds/hit_fast0.ogg") 200 | AudioEngine.load("hit_veryfast", "sounds/hit_veryfast0.ogg") 201 | AudioEngine.load("bounce_synth", "sounds/bounce_synth0.ogg") 202 | for (i in 0...5) { 203 | AudioEngine.load("bounce%(i)", "sounds/bounce%(i).ogg") 204 | } 205 | for (i in 0...5) { 206 | AudioEngine.load("hit%(i)", "sounds/hit%(i).ogg") 207 | } 208 | } 209 | update() { 210 | (_bats + [_ball] + _impacts).each {|obj| obj.update() } 211 | 212 | _impacts = _impacts.where {|impact| impact.time <= 10 }.toList 213 | 214 | if (_ball.out()) { 215 | var scoring_player 216 | if (_ball.pos.x < HALF_W) { 217 | scoring_player = 1 218 | } else { 219 | scoring_player = 0 220 | } 221 | var losing_player = 1 - scoring_player 222 | if (_bats[losing_player].timer < 0) { 223 | _bats[scoring_player].score = _bats[scoring_player].score + 1 224 | AudioEngine.play("score_goal") 225 | _bats[losing_player].timer = 20 226 | } else if (_bats[losing_player].timer == 0) { 227 | var direction 228 | if (losing_player == 0) { 229 | direction = -1 230 | } else { 231 | direction = 1 232 | } 233 | _ball = Ball.new(this, direction) 234 | } 235 | 236 | } 237 | } 238 | draw(alpha) { 239 | _table.draw(0, 0) 240 | for (p in 0..1) { 241 | if (_bats[p].timer > 0 && _ball.out()) { 242 | Canvas.draw(ImageData.loadFromFile("images/effect%(p).png"), 0, 0) 243 | } 244 | } 245 | (_bats + [_ball] + _impacts).each {|obj| obj.draw(alpha) } 246 | for (p in 0..1) { 247 | var score 248 | if (_bats[p].score < 10) { 249 | score = "0%(_bats[p].score)" 250 | } else { 251 | score = "%(_bats[p].score)" 252 | } 253 | for (i in 0..1) { 254 | var color = "0" 255 | var other_p = 1 - p 256 | if (_bats[other_p].timer > 0 && _ball.out()) { 257 | if (p == 0) { 258 | color = "2" 259 | } else { 260 | color = "1" 261 | } 262 | } 263 | var image = "digit%(color)%(score[i])" 264 | Canvas.draw(ImageData.loadFromFile("images/%(image).png"), 255 + (160 * p + (i * 55)), 46) 265 | } 266 | } 267 | } 268 | impact(pos) { 269 | _impacts.add(Impact.new(pos)) 270 | } 271 | ball { _ball } 272 | bats { _bats } 273 | ai_offset=(v) { _ai_offset = v } 274 | ai_offset { _ai_offset } 275 | } 276 | 277 | var MENU = 1 278 | var PLAY = 2 279 | var GAME_OVER = 3 280 | 281 | class Game { 282 | static init() { 283 | __state = MENU 284 | AudioEngine.load("music", "music/theme.ogg") 285 | AudioEngine.load("down", "sounds/down.ogg") 286 | AudioEngine.load("up", "sounds/up.ogg") 287 | var music = AudioEngine.play("music") 288 | music.loop = true 289 | music.volume = 0.3 290 | __game = GameState.init(0) 291 | __playerCount = 1 292 | __pressed = false 293 | } 294 | static update() { 295 | if (Keyboard.isKeyDown("escape")) { 296 | Process.exit() 297 | } 298 | __game.update() 299 | var down = Keyboard.isKeyDown("space") 300 | __pressed = !__pressed && down 301 | 302 | if (__state == MENU) { 303 | if (__pressed) { 304 | __state = PLAY 305 | __game = GameState.init(__playerCount) 306 | } else { 307 | if (__playerCount == 2 && Keyboard.isKeyDown("up")) { 308 | AudioEngine.play("up") 309 | __playerCount = 1 310 | } else if (__playerCount == 1 && Keyboard.isKeyDown("down")) { 311 | AudioEngine.play("down") 312 | __playerCount = 2 313 | } 314 | __game.update() 315 | } 316 | } else if (__state == PLAY) { 317 | if (M.max(__game.bats[0].score, __game.bats[1].score) > 9) { 318 | __state = GAME_OVER 319 | } else { 320 | __game.update() 321 | } 322 | } else if (__state == GAME_OVER) { 323 | if (__pressed) { 324 | __pressed = false 325 | __state = MENU 326 | __playerCount = 1 327 | __game = GameState.init(0) 328 | } 329 | } 330 | __pressed = down 331 | } 332 | static draw(alpha) { 333 | __game.draw(alpha) 334 | if (__state == MENU) { 335 | ImageData.loadFromFile("images/menu%(__playerCount-1).png").draw(0, 0) 336 | } else if (__state == GAME_OVER) { 337 | ImageData.loadFromFile("images/over.png").draw(0, 0) 338 | } 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /boing/music/theme.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/music/theme.ogg -------------------------------------------------------------------------------- /boing/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/screenshot.png -------------------------------------------------------------------------------- /boing/sounds/bounce0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce0.ogg -------------------------------------------------------------------------------- /boing/sounds/bounce1.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce1.ogg -------------------------------------------------------------------------------- /boing/sounds/bounce2.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce2.ogg -------------------------------------------------------------------------------- /boing/sounds/bounce3.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce3.ogg -------------------------------------------------------------------------------- /boing/sounds/bounce4.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce4.ogg -------------------------------------------------------------------------------- /boing/sounds/bounce_synth0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/bounce_synth0.ogg -------------------------------------------------------------------------------- /boing/sounds/down.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/down.ogg -------------------------------------------------------------------------------- /boing/sounds/hit0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit0.ogg -------------------------------------------------------------------------------- /boing/sounds/hit1.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit1.ogg -------------------------------------------------------------------------------- /boing/sounds/hit2.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit2.ogg -------------------------------------------------------------------------------- /boing/sounds/hit3.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit3.ogg -------------------------------------------------------------------------------- /boing/sounds/hit4.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit4.ogg -------------------------------------------------------------------------------- /boing/sounds/hit_fast0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit_fast0.ogg -------------------------------------------------------------------------------- /boing/sounds/hit_medium0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit_medium0.ogg -------------------------------------------------------------------------------- /boing/sounds/hit_slow0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit_slow0.ogg -------------------------------------------------------------------------------- /boing/sounds/hit_synth0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit_synth0.ogg -------------------------------------------------------------------------------- /boing/sounds/hit_veryfast0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/hit_veryfast0.ogg -------------------------------------------------------------------------------- /boing/sounds/score_goal0.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/score_goal0.ogg -------------------------------------------------------------------------------- /boing/sounds/up.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/boing/sounds/up.ogg -------------------------------------------------------------------------------- /boing/zero.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for ImageData 2 | class Actor { 3 | construct new(name, position) { 4 | _pos = position 5 | _name = name 6 | _image = name 7 | } 8 | 9 | name { _name } 10 | image { _image } 11 | image=(v) { _image = v} 12 | pos { _pos } 13 | pos=(p) { _pos = p } 14 | update() {} 15 | draw(alpha) { 16 | Fiber.new { 17 | var image = ImageData.loadFromFile("images/%(image).png") 18 | image.draw(pos.x - image.width / 2, pos.y - image.height / 2) 19 | }.try() 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /fps/DUMMY1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY1.png -------------------------------------------------------------------------------- /fps/DUMMY2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY2.png -------------------------------------------------------------------------------- /fps/DUMMY3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY3.png -------------------------------------------------------------------------------- /fps/DUMMY4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY4.png -------------------------------------------------------------------------------- /fps/DUMMY5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY5.png -------------------------------------------------------------------------------- /fps/DUMMY6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY6.png -------------------------------------------------------------------------------- /fps/DUMMY7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY7.png -------------------------------------------------------------------------------- /fps/DUMMY8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/DUMMY8.png -------------------------------------------------------------------------------- /fps/ceil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/ceil.png -------------------------------------------------------------------------------- /fps/column.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/column.png -------------------------------------------------------------------------------- /fps/column2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/column2.png -------------------------------------------------------------------------------- /fps/context.wren: -------------------------------------------------------------------------------- 1 | import "math" for Vec, M 2 | import "./map" for TileMap, Tile 3 | var VEC = Vec.new() 4 | 5 | /* 6 | class TileMap { 7 | construct new(tiles, width, height) { 8 | _tiles = tiles 9 | _width = width 10 | _height = height 11 | } 12 | 13 | [n] { _tiles[n] } 14 | width { _width } 15 | height { _height } 16 | 17 | } 18 | */ 19 | 20 | class World { 21 | construct new() {} 22 | player { _player } 23 | player=(v) { _player = v } 24 | 25 | doors { _doors } 26 | doors=(v) { _doors = v } 27 | 28 | entities { _entities } 29 | entities=(v) { _entities = v } 30 | 31 | map { _map } 32 | map=(v) { _map = v } 33 | 34 | textures { _textures } 35 | textures=(v) { _textures = v } 36 | 37 | floorTexture { _floorTexture } 38 | floorTexture=(v) { _floorTexture = v } 39 | ceilingTexture { _ceilTexture } 40 | ceilingTexture=(v) { _ceilTexture = v } 41 | 42 | update() { 43 | player.update(this) 44 | entities.each {|sprite| sprite.update(this) } 45 | World.sortSprites(entities, player.pos) 46 | 47 | doors.each {|door| 48 | if ((door.pos - player.pos).length >= 2.75) { 49 | door.close() 50 | } 51 | door.update() 52 | } 53 | } 54 | 55 | getTileAt(position) { 56 | return map.get(position) 57 | // return map.get(position.x.floor, position.y.floor) 58 | /* 59 | var pos = VEC 60 | if (pos.x >= 0 && pos.x < map.width && pos.y >= 0 && pos.y < map.height) { 61 | return map[map.height * pos.y + pos.x] 62 | } 63 | return 1 64 | */ 65 | } 66 | 67 | getDoorAt(position) { 68 | VEC.x = position.x.floor 69 | VEC.y = position.y.floor 70 | var mapPos = VEC 71 | for (door in doors) { 72 | if (door.pos == mapPos) { 73 | return door 74 | } 75 | } 76 | return null 77 | } 78 | 79 | isTileHit(pos) { 80 | VEC.x = pos.x.floor 81 | VEC.y = pos.y.floor 82 | var mapPos = VEC 83 | //var mapPos = Vec.new(pos.x.round, pos.y.round) 84 | var tile = getTileAt(mapPos) 85 | var hit = false 86 | if (tile["door"] == true) { 87 | hit = getDoorAt(mapPos).state > 0.5 88 | } else { 89 | hit = tile["solid"] == true 90 | } 91 | return hit 92 | } 93 | 94 | castRay(result, rayPosition, rayDirection, ignoreDoors) { 95 | var position = player.pos 96 | var direction = player.dir 97 | 98 | var sideDistanceX = (1.0 + rayDirection.y.pow(2) / rayDirection.x.pow(2)).sqrt 99 | var sideDistanceY = (1.0 + rayDirection.x.pow(2) / rayDirection.y.pow(2)).sqrt 100 | 101 | var nextSideDistanceX 102 | var nextSideDistanceY 103 | var mapPos = Vec.new(rayPosition.x.floor, rayPosition.y.floor) 104 | var stepDirection = Vec.new() 105 | if (rayDirection.x < 0) { 106 | stepDirection.x = -1 107 | nextSideDistanceX = (rayPosition.x - mapPos.x) * sideDistanceX 108 | } else { 109 | stepDirection.x = 1 110 | nextSideDistanceX = (mapPos.x + 1.0 - rayPosition.x) * sideDistanceX 111 | } 112 | if (rayDirection.y < 0) { 113 | stepDirection.y = -1 114 | nextSideDistanceY = (rayPosition.y - mapPos.y) * sideDistanceY 115 | } else { 116 | stepDirection.y = 1 117 | nextSideDistanceY = (mapPos.y + 1.0 - rayPosition.y) * sideDistanceY 118 | } 119 | 120 | var hit = false 121 | var side = 0 122 | while (!hit) { 123 | if (nextSideDistanceX < nextSideDistanceY) { 124 | nextSideDistanceX = nextSideDistanceX + sideDistanceX 125 | mapPos.x = (mapPos.x + stepDirection.x) 126 | side = 0 127 | } else { 128 | nextSideDistanceY = nextSideDistanceY + sideDistanceY 129 | mapPos.y = (mapPos.y + stepDirection.y) 130 | side = 1 131 | } 132 | 133 | var tile = getTileAt(mapPos) 134 | if (tile["door"]) { 135 | // Figure out the door position 136 | var doorState = ignoreDoors ? 1 : getDoorAt(mapPos).state 137 | var adj 138 | var ray_mult 139 | // Adjustment 140 | if (side == 0) { 141 | adj = mapPos.x - position.x + 1 142 | if (position.x < mapPos.x) { 143 | adj = adj - 1 144 | } 145 | ray_mult = adj / rayDirection.x 146 | } else { 147 | adj = mapPos.y - position.y 148 | if (position.y > mapPos.y) { 149 | adj = adj + 1 150 | } 151 | ray_mult = adj / rayDirection.y 152 | } 153 | 154 | var rye2 = rayPosition.y + rayDirection.y * ray_mult 155 | var rxe2 = rayPosition.x + rayDirection.x * ray_mult 156 | 157 | var trueDeltaX = sideDistanceX 158 | var trueDeltaY = sideDistanceY 159 | if (rayDirection.y.abs < 0.01) { 160 | trueDeltaY = 100 161 | } 162 | if (rayDirection.x.abs < 0.01) { 163 | trueDeltaX = 100 164 | } 165 | 166 | if (side == 0) { 167 | // var halfY = mapPos.y + sideDistanceY * 0.5 168 | var true_y_step = (trueDeltaX * trueDeltaX - 1).sqrt 169 | var half_step_in_y = rye2 + (stepDirection.y * true_y_step) * 0.5 170 | hit = (half_step_in_y.floor == mapPos.y) && (1 - 2*(half_step_in_y - mapPos.y)).abs > 1 - doorState 171 | } else { 172 | var true_x_step = (trueDeltaY * trueDeltaY - 1).sqrt 173 | var half_step_in_x = rxe2 + (stepDirection.x * true_x_step) * 0.5 174 | hit = (half_step_in_x.floor == mapPos.x) && (1 - 2*(half_step_in_x - mapPos.x)).abs > 1 - doorState 175 | } 176 | } else { 177 | hit = tile["solid"] == true 178 | } 179 | } 180 | result[0] = mapPos 181 | result[1] = side 182 | result[2] = stepDirection 183 | return result 184 | } 185 | 186 | static sortSprites(list, position) { 187 | var i = 1 188 | while (i < list.count) { 189 | var x = list[i] 190 | var j = i - 1 191 | while (j >= 0 && (list[j].pos - position).length < (x.pos - position).length) { 192 | list[j + 1] = list[j] 193 | j = j - 1 194 | } 195 | list[j + 1] = x 196 | i = i + 1 197 | } 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /fps/door.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/door.png -------------------------------------------------------------------------------- /fps/door.wren: -------------------------------------------------------------------------------- 1 | import "math" for M 2 | import "./sprite" for Entity 3 | 4 | class Door is Entity { 5 | construct new(position) { 6 | super(position) 7 | _locked = false 8 | _state = 1 9 | _mode = 0 10 | } 11 | 12 | locked { _locked } 13 | state { _state } 14 | mode { _mode } 15 | 16 | update() { 17 | _state = M.mid(0, _state + _mode * 0.1, 1) 18 | if (_state == 0 || _state == 1) { 19 | _mode = 0 20 | } 21 | } 22 | 23 | open() { 24 | if (!_locked) { 25 | _mode = -1 26 | } 27 | } 28 | 29 | close() { 30 | if (!_locked) { 31 | _mode = 1 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /fps/floor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/floor.png -------------------------------------------------------------------------------- /fps/keys.wren: -------------------------------------------------------------------------------- 1 | /* 2 | InputGroup Library v0.3 3 | Allows for grouping DigitalInput together into a single action, 4 | with allowances for repetition. 5 | */ 6 | import "input" for Keyboard, Mouse, DigitalInput 7 | 8 | class InputGroup { 9 | construct new(inputs, action) { 10 | if (inputs is Sequence) { 11 | _inputs = inputs 12 | } else if (inputs is DigitalInput) { 13 | _inputs = [ inputs ] 14 | } 15 | 16 | _action = action 17 | _repeating = true 18 | _freq = 30 19 | if (!_inputs.all {|input| input is DigitalInput }) { 20 | Fiber.abort("Inputs must be DigitalInput") 21 | } 22 | } 23 | 24 | repeating { _repeating } 25 | repeating=(v) { _repeating = v } 26 | 27 | frequency { _freq } 28 | frequency=(v) { _freq = v } 29 | 30 | down { 31 | return _inputs.count > 0 && _inputs.any {|input| input.down } 32 | } 33 | 34 | firing { 35 | return _inputs.count > 0 && _inputs.any {|input| 36 | if (_repeating) { 37 | return input.down && input.repeats % _freq == 0 38 | } else { 39 | return input.justPressed 40 | } 41 | } 42 | } 43 | 44 | action { _action } 45 | } 46 | -------------------------------------------------------------------------------- /fps/main.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for Color, Canvas, ImageData 2 | import "dome" for Window, Process 3 | import "math" for Vec, M 4 | import "input" for Keyboard, Mouse 5 | import "./keys" for InputGroup 6 | import "./sprite" for Sprite, Pillar, Player, Person 7 | import "./door" for Door 8 | import "./context" for World, TileMap, Tile 9 | import "./texture" for Texture 10 | import "./renderer" for Renderer 11 | 12 | var SPEED = 0.001 13 | var Interact = InputGroup.new([ Mouse["left"], Keyboard["e"], Keyboard["space"] ], SPEED) 14 | var Forward = InputGroup.new(Keyboard["w"], SPEED) 15 | var Back = InputGroup.new(Keyboard["s"], -SPEED) 16 | var LeftBtn = InputGroup.new(Keyboard["a"], -SPEED) 17 | var RightBtn = InputGroup.new(Keyboard["d"], SPEED) 18 | var StrafeLeftBtn = InputGroup.new(Keyboard["left"], -1) 19 | var StrafeRightBtn = InputGroup.new(Keyboard["right"], 1) 20 | 21 | var DOORS = [ 22 | Door.new(Vec.new(2, 11)), 23 | Door.new(Vec.new(3, 13)) 24 | ] 25 | 26 | var MAP_WIDTH = 30 27 | var MAP_HEIGHT = 30 28 | var MAP = [ 29 | 2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 30 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 31 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 32 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 33 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 34 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 35 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 36 | 6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 37 | 2,0,0,0,0,0,0,0,0,0,0,3,1,1,1,1,1,3,0,0,0,0,0,0,0,0,0,0,0,2, 38 | 2,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2, 39 | 2,0,2,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2, 40 | 2,0,5,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2, 41 | 2,0,2,0,0,0,0,0,0,0,0,3,1,1,0,1,1,3,0,0,0,0,0,0,0,0,0,0,0,2, 42 | 2,0,2,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 43 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 44 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 45 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 46 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 47 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 48 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 49 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 50 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 51 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 52 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 53 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 54 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 55 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 56 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 57 | 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, 58 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 59 | ] 60 | 61 | 62 | 63 | class Game { 64 | static init() { 65 | var SCALE = 3 66 | Canvas.resize(320, 200) 67 | Window.resize(SCALE*Canvas.width, SCALE*Canvas.height) 68 | 69 | Mouse.relative = true 70 | Mouse.hidden = true 71 | 72 | __player = Player.new(Vec.new(7, 11), 0) 73 | __sprites = [ 74 | //Pillar.new(Vec.new(8, 13)), 75 | Pillar.new(Vec.new(9, 15)), 76 | Person.new(Vec.new(8, 13)) 77 | ] 78 | __doors = DOORS 79 | __map = TileMap.new(MAP_WIDTH, MAP_HEIGHT) 80 | for (y in 0...MAP_HEIGHT) { 81 | for (x in 0...MAP_WIDTH) { 82 | var type = MAP[y * MAP_WIDTH + x] 83 | __map.set(x, y, Tile.new(type, { 84 | "solid": type != 0, 85 | "door": type == 5 86 | })) 87 | } 88 | } 89 | __camera = __player.dir.perp 90 | __angle = 0 91 | 92 | __world = World.new() 93 | __world.entities = __sprites 94 | __world.doors = __doors 95 | __world.player = __player 96 | __world.map = __map 97 | __textures = [] 98 | __world.textures = __textures 99 | // __world.floorTexture = Texture.importImg("floor.png") 100 | //__world.ceilingTexture = Texture.importImg("ceil.png") 101 | __renderer = Renderer.init(__world, 320, 200) 102 | 103 | // Map data 104 | // - Map arrangement 105 | // - Textures for map 106 | 107 | // Prepare textures 108 | for (i in 1..4) { 109 | __textures.add(Texture.importImg("wall%(i).png")) 110 | } 111 | __textures.add(Texture.importImg("door.png")) 112 | } 113 | 114 | static update() { 115 | if (Keyboard.isKeyDown("escape")) { 116 | Process.exit() 117 | } 118 | 119 | var angle = __player.angle 120 | angle = angle + M.mid(-2, Mouse.x / 2, 2) 121 | if (StrafeLeftBtn.down) { 122 | angle = angle + StrafeLeftBtn.action 123 | } 124 | if (StrafeRightBtn.down) { 125 | angle = angle + StrafeRightBtn.action 126 | } 127 | __player.angle = angle 128 | 129 | __camera.x = -__player.dir.y 130 | __camera.y = __player.dir.x 131 | 132 | var vel = __player.vel 133 | vel.x = 0 134 | vel.y = 0 135 | 136 | if (RightBtn.down) { 137 | vel = vel + __camera 138 | } 139 | if (LeftBtn.down) { 140 | vel = vel - __camera 141 | } 142 | if (Forward.down) { 143 | vel = vel + __player.dir 144 | } 145 | if (Back.down) { 146 | vel = vel - __player.dir 147 | } 148 | __player.vel = vel 149 | 150 | __world.update() 151 | 152 | var targetPos = __player.getTarget(__world) 153 | var dist = targetPos - __player.pos 154 | 155 | if (Interact.firing) { 156 | var door = __world.getDoorAt(targetPos) 157 | if (door != null && dist.length < 2.75) { 158 | door.open() 159 | } 160 | } 161 | 162 | __dirty = true 163 | __renderer.update() 164 | } 165 | 166 | static draw(alpha) { 167 | __renderer.draw() 168 | 169 | var centerX = Canvas.width / 2 170 | var centerY = Canvas.height / 2 171 | 172 | Canvas.line(centerX - 4, centerY, centerX + 4, centerY, Color.green, 1) 173 | Canvas.line(centerX, centerY - 4, centerX, centerY + 4, Color.green, 1) 174 | 175 | //ms = (end - start) 176 | //ms = ms / counter 177 | Canvas.print(__angle, 0, 0, Color.white) 178 | __dirty = false 179 | } 180 | 181 | static sortSprites(list, position) { 182 | var i = 1 183 | while (i < list.count) { 184 | var x = list[i] 185 | var j = i - 1 186 | while (j >= 0 && (list[j].pos - position).length < (x.pos - position).length) { 187 | list[j + 1] = list[j] 188 | j = j - 1 189 | } 190 | list[j + 1] = x 191 | i = i + 1 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /fps/map.wren: -------------------------------------------------------------------------------- 1 | import "math" for Vec, M 2 | 3 | class Elegant { 4 | static pair(vec) { pair(vec.x, vec.y) } 5 | static pair(x, y) { 6 | if (x >= y) { 7 | return x * x + x + y 8 | } else { 9 | return y * y + x 10 | } 11 | } 12 | 13 | static unpair(z) { 14 | var sqrtz = M.floor(z.sqrt) 15 | var sqz = sqrtz * sqrtz 16 | if ((z - sqz) > sqrtz) { 17 | return Vec.new(sqrtz, z - sqz - sqrtz) 18 | } else { 19 | return Vec.new(z- sqz, sqrtz) 20 | } 21 | } 22 | } 23 | 24 | class Tile { 25 | construct new() { 26 | init_(0, {}) 27 | } 28 | construct new(texture) { 29 | init_(texture, {}) 30 | } 31 | construct new(texture, data) { 32 | init_(texture, data) 33 | } 34 | 35 | toString { "Tile: %(texture), %(data)" } 36 | 37 | copy { 38 | var data = {} 39 | for (key in _data.keys) { 40 | data[key] = _data[key] 41 | } 42 | return Tile.new(_texture, data) 43 | } 44 | 45 | init_(texture, data) { 46 | _texture = texture 47 | _data = data 48 | } 49 | 50 | texture { _texture } 51 | 52 | data { _data } 53 | [index] { _data[index] } 54 | [index]=(v) { _data[index] = v } 55 | } 56 | 57 | var VOID_TILE = Tile.new(-1, { "solid": true }) 58 | var EMPTY_TILE = Tile.new(0) 59 | 60 | class TileMap { 61 | construct new(width, height, tile) { 62 | _width = width 63 | _height = height 64 | _tiles = List.filled(_width * _height, tile.copy) 65 | } 66 | construct new(width, height) { 67 | _width = width 68 | _height = height 69 | _tiles = List.filled(_width * _height, EMPTY_TILE) 70 | } 71 | 72 | clear(vec) { clear(vec.x, vec.y) } 73 | clear(x, y) { 74 | set(x, y, EMPTY_TILE) 75 | } 76 | get(vec) { get(vec.x, vec.y) } 77 | get(x, y) { 78 | x = x.floor 79 | y = y.floor 80 | if (x < 0 || x >= width || y < 0 || y >= height) { 81 | return VOID_TILE 82 | } 83 | return _tiles[_width * y + x] 84 | } 85 | 86 | set(vec, tile) { setTile(vec.x, vec.y, tile) } 87 | set(x, y, tile) { 88 | x = x.floor 89 | y = y.floor 90 | if (x < 0 || x >= width || y < 0 || y >= height) { 91 | Fiber.abort("Tile index out of bounds (%(x),%(y))") 92 | } 93 | if (!tile is Tile) { 94 | Fiber.abort("Only instances of Tile can be added to the tilemap") 95 | } 96 | _tiles[_width * y + x] = tile 97 | } 98 | 99 | width { _width } 100 | height { _height } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /fps/renderer.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for ImageData, Canvas, Color 2 | import "math" for Vec, M 3 | 4 | var alpha = 0.5 5 | var VEC = Vec.new() 6 | var NULL_COLOR = Color.pink 7 | var DARK_NULL_COLOR = Color.rgb(NULL_COLOR.r * alpha, NULL_COLOR.g * alpha, NULL_COLOR.b * alpha) 8 | 9 | class Renderer { 10 | construct init(world, width, height) { 11 | _world = world 12 | _w = width 13 | _h = height 14 | _halfW = _w / 2 15 | _halfH = _h / 2 16 | _canvas = ImageData.create("buffer", _w, _h) 17 | 18 | _camera = Vec.new(-1, 0) 19 | _rayBuffer = List.filled(_w, null).map { [0, 0, 0, 0] }.toList 20 | _DIST_LOOKUP = [] 21 | for (y in 0..._h) { 22 | _DIST_LOOKUP.add(_h / (2.0 * y - _h)) 23 | } 24 | _dirty = true 25 | floors = _world.floorTexture 26 | ceilings = _world.ceilingTexture 27 | } 28 | 29 | width { _w } 30 | height { _h } 31 | floors=(v) { 32 | _drawFloor = v != null && !(v is Color) 33 | _floorTexture = v != null ? v : Color.darkgray 34 | } 35 | ceilings=(v) { 36 | _drawCeiling = v != null && !(v is Color) 37 | _ceilingTexture = v != null ? v : Color.lightgray 38 | } 39 | 40 | update() { 41 | _camera.x = -_world.player.dir.y 42 | _camera.y = _world.player.dir.x 43 | var position = _world.player.pos 44 | var direction = _world.player.dir 45 | 46 | if (_dirty) { 47 | var rayPosition = position 48 | var castResult = [0, 0, 0] 49 | for (x in 0..._w) { 50 | var cameraX = 2 * (x / _w) - 1 51 | var rayDirection = direction + (_camera * cameraX) 52 | _world.castRay(castResult, rayPosition, rayDirection, false) 53 | var mapPos = castResult[0] 54 | var side = castResult[1] 55 | var stepDirection = castResult[2] 56 | var tile = _world.getTileAt(mapPos) 57 | 58 | var perpWallDistance 59 | if (tile["door"] == true) { 60 | // If it's a door, we need to shift the map position to draw it in the correct location 61 | if (side == 0) { 62 | mapPos.x = mapPos.x + stepDirection.x / 2 63 | } else { 64 | mapPos.y = mapPos.y + stepDirection.y / 2 65 | } 66 | } 67 | if (side == 0) { 68 | perpWallDistance = M.abs((mapPos.x - position.x + (1 - stepDirection.x) / 2) / rayDirection.x) 69 | } else { 70 | perpWallDistance = M.abs((mapPos.y - position.y + (1 - stepDirection.y) / 2) / rayDirection.y) 71 | } 72 | //SET THE ZBUFFER FOR THE SPRITE CASTING 73 | var ray = _rayBuffer[x] 74 | ray[0] = perpWallDistance 75 | ray[1] = mapPos 76 | ray[2] = side 77 | ray[3] = rayDirection 78 | } 79 | } 80 | 81 | } 82 | 83 | 84 | draw() { 85 | if (!_dirty) { 86 | return 87 | } 88 | var start 89 | start = System.clock 90 | var end 91 | cls() 92 | 93 | var ms = 0 94 | // Floor casting 95 | // rayDir for leftmost ray (x = 0) and rightmost ray (x = w) 96 | var localStart 97 | var localEnd 98 | var counter = 0 99 | // ms = ms / counter * 1000 100 | 101 | // Wall casting 102 | var rayPosition = _world.player.pos 103 | for (x in 0..._w) { 104 | counter = counter + 1 105 | var ray = _rayBuffer[x] 106 | var perpWallDistance = ray[0] 107 | var mapPos = ray[1] 108 | var side = ray[2] 109 | var rayDirection = ray[3] 110 | 111 | var color = Color.black 112 | var tile = _world.getTileAt(mapPos) 113 | var texture 114 | Fiber.new { 115 | texture = _world.textures[tile.texture - 1] 116 | }.try() 117 | 118 | var lineHeight = M.abs(_h / perpWallDistance) 119 | var drawStart = (-lineHeight / 2) + (_halfH) 120 | var drawEnd = (lineHeight / 2) + (_halfH) 121 | // If we are too close to a block, the lineHeight is gigantic, resulting in slowness 122 | // So we clip the drawStart-End and _then_ calculate the texture position. 123 | drawStart = M.max(0, drawStart) 124 | drawEnd = M.min(_h, drawEnd) 125 | 126 | var wallX 127 | if (side == 0) { 128 | wallX = rayPosition.y + perpWallDistance * rayDirection.y 129 | } else { 130 | wallX = rayPosition.x + perpWallDistance * rayDirection.x 131 | } 132 | wallX = wallX - wallX.floor 133 | 134 | /* 135 | UNTEXTURED COLOR CHOOSER 136 | */ 137 | if (texture == null) { 138 | // WORST CASE 139 | color = NULL_COLOR 140 | if (side == 1) { 141 | color = DARK_NULL_COLOR 142 | } 143 | for (y in drawStart...drawEnd) { 144 | _canvas.pset(x, y, color) 145 | } 146 | } else { 147 | var texWidth = texture.width 148 | var texX = wallX * texWidth 149 | if (side == 0 && rayDirection.x <= 0) { 150 | texX = texWidth - texX 151 | } 152 | if (side == 1 && rayDirection.y > 0) { 153 | texX = texWidth - texX 154 | } 155 | texX = texX.floor 156 | var texStep = 1.0 * texture.height / lineHeight 157 | var texPos = (drawStart - _halfH + lineHeight / 2) * texStep 158 | for (y in drawStart...drawEnd) { 159 | var texY = (texPos).floor 160 | if (side == 1) { 161 | color = texture.pgetDark(texX, texY) 162 | } else { 163 | color = texture.pget(texX, texY) 164 | } 165 | _canvas.pset(x, y, color) 166 | texPos = texPos + texStep 167 | } 168 | } 169 | if (_drawFloor || _drawCeiling) { 170 | var floorXWall 171 | var floorYWall 172 | if (side == 0 && rayDirection.x > 0) { 173 | floorXWall = mapPos.x 174 | floorYWall = mapPos.y + wallX 175 | } else if (side == 0 && rayDirection.x < 0) { 176 | floorXWall = mapPos.x + 1.0 177 | floorYWall = mapPos.y + wallX 178 | } else if (side == 1 && rayDirection.y > 0) { 179 | floorXWall = mapPos.x + wallX 180 | floorYWall = mapPos.y 181 | } else { 182 | floorXWall = mapPos.x + wallX 183 | floorYWall = mapPos.y + 1.0 184 | } 185 | var distWall = perpWallDistance 186 | var floorTex = _floorTexture 187 | var ceilTex = _ceilingTexture 188 | drawEnd = drawEnd.floor 189 | localStart = System.clock 190 | for (y in (drawEnd)..._h) { 191 | var currentDist = _DIST_LOOKUP[y.floor] 192 | var weight = currentDist / distWall 193 | var currentFloorX = weight * floorXWall + (1.0 - weight) * rayPosition.x 194 | var currentFloorY = weight * floorYWall + (1.0 - weight) * rayPosition.y 195 | var c 196 | var floorTexX 197 | var floorTexY 198 | var ceilTexX 199 | var ceilTexY 200 | if (_drawFloor && _drawCeiling) { 201 | floorTexX = ((currentFloorX * (floorTex.width)).floor % (floorTex.width)) 202 | if (floorTex.width == ceilTex.width) { 203 | ceilTexX = floorTexX 204 | } else { 205 | ceilTexX = ((currentFloorX * (ceilTex.width)).floor % ceilTex.width) 206 | } 207 | floorTexY = ((currentFloorY * (floorTex.height)).floor % (floorTex.height)) 208 | if (floorTex.height == ceilTex.height) { 209 | ceilTexY = floorTexY 210 | } else { 211 | ceilTexY = ((currentFloorY * (ceilTex.height)).floor % ceilTex.height) 212 | } 213 | } else if (_drawFloor) { 214 | floorTexX = ((currentFloorX * (floorTex.width)).floor % (floorTex.width)) 215 | floorTexY = ((currentFloorY * (floorTex.height)).floor % (floorTex.height)) 216 | } else if (_drawCeiling) { 217 | ceilTexX = ((currentFloorX * (ceilTex.width)).floor % ceilTex.width) 218 | ceilTexY = ((currentFloorY * (ceilTex.height)).floor % ceilTex.height) 219 | } 220 | if (_drawFloor) { 221 | c = floorTex.pget(floorTexX, floorTexY) 222 | _canvas.pset(x.floor, y.floor, c) 223 | } 224 | if (_drawCeiling) { 225 | c = ceilTex.pget(ceilTexX, ceilTexY) 226 | _canvas.pset(x.floor, (_h - y.floor - 1), c) 227 | } 228 | /* 229 | 230 | if (_drawFloor) { 231 | var floorTexX = ((currentFloorX * (floorTex.width)).floor % (floorTex.width)) 232 | var floorTexY = ((currentFloorY * (floorTex.height)).floor % (floorTex.height)) 233 | c = floorTex.pget(floorTexX, floorTexY) 234 | _canvas.pset(x.floor, y.floor, c) 235 | } 236 | if (_drawCeiling) { 237 | var ceilTexX = ((currentFloorX * (ceilTex.width)).floor % ceilTex.width) 238 | var ceilTexY = ((currentFloorY * (ceilTex.height)).floor % ceilTex.height) 239 | 240 | c = ceilTex.pget(ceilTexX, ceilTexY) 241 | _canvas.pset(x.floor, (_h - y.floor - 1), c) 242 | } 243 | */ 244 | } 245 | localEnd = System.clock 246 | ms = ms + (localEnd - localStart) 247 | } 248 | } 249 | 250 | var dir = _world.player.dir 251 | var cam = -_camera 252 | var invDet = 1.0 / (-cam.x * dir.y + dir.x * cam.y) 253 | 254 | for (sprite in _world.entities) { 255 | var uDiv = sprite.uDiv 256 | var vDiv = sprite.vDiv 257 | var vMove = sprite.vMove 258 | 259 | var spriteX = sprite.pos.x - rayPosition.x 260 | var spriteY = sprite.pos.y - rayPosition.y 261 | 262 | var transformX = invDet * (dir.y * spriteX - dir.x * spriteY) 263 | //this is actually the depth inside the screen, that what Z is in 3D 264 | var transformY = invDet * (cam.y * spriteX - cam.x * spriteY) 265 | 266 | var vMoveScreen = (vMove / transformY).floor 267 | 268 | var spriteScreenX = ((_halfW) * (1 + transformX / transformY)).floor 269 | // Prevent fisheye 270 | var spriteHeight = ((_h / transformY).abs / vDiv).floor 271 | var drawStartY = (((_h - spriteHeight) / 2) + vMoveScreen).floor 272 | if (drawStartY < 0) { 273 | drawStartY = 0 274 | } 275 | var drawEndY = (((_h + spriteHeight) / 2) + vMoveScreen).floor 276 | if (drawEndY >= _h) { 277 | drawEndY = _h 278 | } 279 | 280 | // Optimisation note: this is actually half of spriteWidth, because we typically divide it by 2 281 | var spriteWidth = (((_h / transformY).abs / uDiv) / 2).floor 282 | var drawStartX = (spriteScreenX - spriteWidth).floor 283 | if (drawStartX < 0) { 284 | drawStartX = 0 285 | } 286 | var drawEndX = (spriteScreenX + spriteWidth).floor 287 | if (drawEndX >= _w) { 288 | drawEndX = _w - 1 289 | } 290 | 291 | var texture = sprite.currentTex 292 | var texWidth = texture.width - 1 293 | var texHeight = texture.height - 1 294 | for (stripe in drawStartX...drawEndX) { 295 | var texX = ((stripe - (-spriteWidth + spriteScreenX)) * texWidth / (spriteWidth * 2)).abs 296 | 297 | // Conditions for this if: 298 | //1) it's in front of camera plane so you don't see things behind you 299 | //2) it's on the screen (left) 300 | //3) it's on the screen (right) 301 | //4) ZBuffer, with perpendicular distance 302 | if (transformY > 0 && stripe > 0 && stripe < _w && transformY < _rayBuffer[stripe][0]) { 303 | for (y in drawStartY...drawEndY) { 304 | var texY = (((y - vMoveScreen) - (-spriteHeight / 2 + _h / 2)) * texHeight / spriteHeight).abs 305 | var color = texture.pget(texX, texY) 306 | if (color.a != 0) { 307 | _canvas.pset(stripe, y.floor, color) 308 | if (stripe == 1) { 309 | _canvas.pset(0, y.floor, color) 310 | } 311 | } 312 | } 313 | } 314 | } 315 | } 316 | flip() 317 | Canvas.print(ms * 1000, 0, _h - 8, Color.white) 318 | } 319 | 320 | cls() { 321 | if (!(_drawFloor && _drawCeiling)) { 322 | for (y in 0..._halfH) { 323 | for (x in 0..._w) { 324 | var c 325 | if (!_drawCeiling) { 326 | c = _ceilingTexture 327 | _canvas.pset(x, y, c) 328 | } 329 | if (!_drawFloor) { 330 | c = _floorTexture 331 | _canvas.pset(x, _h - y - 1, c) 332 | } 333 | } 334 | } 335 | } 336 | } 337 | 338 | flip() { 339 | Canvas.draw(_canvas, 0, 0) 340 | } 341 | } 342 | -------------------------------------------------------------------------------- /fps/sprite.wren: -------------------------------------------------------------------------------- 1 | import "./texture" for Texture 2 | import "math" for M, Vec 3 | import "input" for Keyboard, Mouse 4 | import "./keys" for InputGroup 5 | 6 | var PI_RAD = Num.pi / 180 7 | var VEC = Vec.new() 8 | var CAST_RESULT = [0, 0, 0, 0] 9 | var MOVE_SPEED = 2/ 60 10 | 11 | class Entity { 12 | construct new(position) { 13 | _pos = position 14 | } 15 | solid { false } 16 | pos { _pos } 17 | pos=(v) { _pos = v } 18 | 19 | update(context) {} 20 | draw() {} 21 | } 22 | 23 | class Player is Entity { 24 | construct new(position, angle) { 25 | super(position) 26 | _dir = Vec.new() 27 | _vel = Vec.new() 28 | this.angle = angle 29 | } 30 | 31 | angle { _angle } 32 | angle=(v) { 33 | _angle = v % 360 34 | if (_angle < 0) { 35 | _angle = _angle + 360 36 | } 37 | _dir.x = M.cos(_angle * PI_RAD) 38 | _dir.y = M.sin(_angle * PI_RAD) 39 | } 40 | 41 | update(context) { 42 | this.vel = this.vel.unit * MOVE_SPEED 43 | 44 | var solid 45 | var oldPosition = VEC 46 | oldPosition.x = this.pos.x 47 | oldPosition.y = this.pos.y 48 | 49 | this.pos.x = this.pos.x + this.vel.x 50 | solid = context.isTileHit(pos) 51 | if (!solid) { 52 | for (entity in context.entities) { 53 | if ((entity.pos - this.pos).length < 0.5) { 54 | solid = solid || entity.solid 55 | } 56 | if (solid) { 57 | break 58 | } 59 | } 60 | } 61 | if (solid) { 62 | this.pos.x = oldPosition.x 63 | this.pos.y = oldPosition.y 64 | } 65 | 66 | oldPosition.x = this.pos.x 67 | oldPosition.y = this.pos.y 68 | 69 | this.pos.y = this.pos.y + this.vel.y 70 | solid = context.isTileHit(this.pos) 71 | if (!solid) { 72 | for (entity in context.entities) { 73 | if ((entity.pos - this.pos).length < 0.5) { 74 | solid = solid || entity.solid 75 | } 76 | if (solid) { 77 | break 78 | } 79 | } 80 | } 81 | if (solid) { 82 | this.pos.x = oldPosition.x 83 | this.pos.y = oldPosition.y 84 | } 85 | oldPosition.x = this.pos.x 86 | oldPosition.y = this.pos.y 87 | } 88 | 89 | getTarget(context) { 90 | context.castRay(CAST_RESULT, pos, dir, true) 91 | return CAST_RESULT[0] 92 | } 93 | 94 | dir { _dir } 95 | vel { _vel } 96 | vel=(v) { _vel = v } 97 | 98 | } 99 | 100 | class Sprite is Entity { 101 | construct new(pos, textures) { 102 | super(pos) 103 | if (!(textures is List)) { 104 | textures = [ textures ] 105 | } 106 | _textures = textures 107 | _octant = 0 108 | } 109 | 110 | textures { _textures } 111 | octant { _octant } 112 | octant=(v) { _octant = v % _textures.count } 113 | currentTex { _textures[octant] } 114 | 115 | uDiv { 1 } 116 | vDiv { 1 } 117 | vMove { 0 } 118 | } 119 | 120 | class DirectionalSprite is Sprite { 121 | construct new(pos, textures) { 122 | super(pos, textures) 123 | _segmentSize = 360 / this.textures.count 124 | } 125 | 126 | update(context) { 127 | super(context) 128 | var playerPos = pos - context.player.pos 129 | var angle = (playerPos.y.atan(playerPos.x) / PI_RAD + 360 - _segmentSize / 2) % 360 130 | octant = (angle / _segmentSize).round 131 | } 132 | } 133 | 134 | class Person is DirectionalSprite { 135 | construct new(pos) { 136 | super(pos, (8..1).map {|n| Texture.importImg("./DUMMY%(n).png")}.toList) 137 | } 138 | solid { true } 139 | } 140 | class Pillar is DirectionalSprite { 141 | construct new(pos) { 142 | super(pos, Texture.importImg("./column.png")) 143 | } 144 | solid { true } 145 | vMove { 0 } 146 | vDiv { 1 } 147 | } 148 | -------------------------------------------------------------------------------- /fps/texture.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for ImageData, Color 2 | import "math" for M 3 | 4 | class Texture { 5 | 6 | // Round-tripping to fetch image data is too slow, so we import the image data into 7 | // Wren memory for faster retrieval. 8 | construct new(data, width, height) { 9 | _data = data 10 | _width = width 11 | _height = height 12 | _iwidth = width - 1 13 | _iheight = height - 1 14 | var alpha = 0.5 15 | _darker = [] 16 | for (color in _data) { 17 | var newColor = Color.rgb(color.r * alpha, color.g * alpha, color.b * alpha, color.a) 18 | _darker.add(newColor) 19 | } 20 | } 21 | 22 | [n] { _data[n] } 23 | width { _width } 24 | height { _height } 25 | 26 | pget(x, y) { 27 | x = x.round % _width 28 | y = y.round % _height 29 | // x = M.mid(0, x, _iwidth).round 30 | // y = M.mid(0, y, _iheight).round 31 | return _data[y * width + x] 32 | } 33 | pgetDark(x, y) { 34 | x = x.round % _width 35 | y = y.round % _height 36 | //x = M.mid(0, x, _iwidth).round 37 | //y = M.mid(0, y, _iheight).round 38 | return _darker[y * width + x] 39 | } 40 | 41 | static importImg(path) { 42 | var texture = [] 43 | var img = ImageData.loadFromFile(path) 44 | for (y in 0...img.height) { 45 | for (x in 0...img.width) { 46 | texture.add(img.pget(x,y)) 47 | } 48 | } 49 | return Texture.new(texture, img.width, img.height) 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /fps/wall1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/wall1.png -------------------------------------------------------------------------------- /fps/wall2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/wall2.png -------------------------------------------------------------------------------- /fps/wall3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/wall3.png -------------------------------------------------------------------------------- /fps/wall4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/fps/wall4.png -------------------------------------------------------------------------------- /platformer/keys.wren: -------------------------------------------------------------------------------- 1 | import "input" for Keyboard 2 | 3 | class Key { 4 | construct new(key, result) { 5 | _name = key 6 | _repeatable = true 7 | _wasPressed = false 8 | _result = result 9 | _counter = 0 10 | } 11 | construct new(key, result, repeatable) { 12 | _name = key 13 | _repeatable = repeatable 14 | _counter = 0 15 | _wasPressed = false 16 | _result = result 17 | } 18 | 19 | name { _name } 20 | result { _result } 21 | 22 | update() { 23 | var isPressed = Keyboard.isKeyDown(_name) 24 | var fire 25 | if (_repeatable) { 26 | fire = isPressed && (_counter == 0) 27 | if (!isPressed) { 28 | _counter = 0 29 | } else if (fire) { 30 | _counter = 8 31 | } else { 32 | _counter = _counter - 1 33 | } 34 | } else { 35 | fire = !_wasPressed && isPressed 36 | _wasPressed = isPressed 37 | } 38 | 39 | return fire 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /platformer/main.wren: -------------------------------------------------------------------------------- 1 | import "dome" for Process 2 | import "graphics" for Canvas, Color 3 | import "input" for Keyboard 4 | import "math" for Vec, M 5 | import "./keys" for Key 6 | 7 | // Physics Constants 8 | var JUMP = 3 9 | var GRAVITY = 0.2 10 | var FRICTION = 0.16 11 | 12 | var MAX_SPEED = 1 13 | var MOVE_FORCE = 0.08 14 | var CHANGE_FORCE = 0.5 15 | 16 | // World space 17 | var TILE_SIZE = 8 18 | 19 | class Tile { 20 | construct new(type, solid) { 21 | _type = type 22 | _solid = solid 23 | _oneway = false 24 | } 25 | construct new(type, solid, oneway) { 26 | _type = type 27 | _solid = solid 28 | _oneway = oneway 29 | } 30 | type { _type } 31 | solid { _solid } 32 | oneway { _oneway } 33 | } 34 | 35 | class BasicTilemap { 36 | construct init(width, height) { 37 | _width = width 38 | _height = height 39 | _tiles = List.filled(_width * _height, Tile.new(0, false)) 40 | } 41 | 42 | get(vec) { get(vec.x, vec.y) } 43 | get(x, y) { 44 | if (x < 0 || x >= width || y < 0 || y >= height) { 45 | return Tile.new(null, true) 46 | } 47 | return _tiles[_height * y + x] 48 | } 49 | 50 | set(vec, tile) { setTile(vec.x, vec.y, tile) } 51 | set(x, y, tile) { 52 | if (x < 0 || x >= width || y < 0 || y >= height) { 53 | Fiber.abort("Tile index out of bounds (%(x),%(y))") 54 | } 55 | if (!tile is Tile) { 56 | Fiber.abort("Only instances of Tile can be added to the tilemap") 57 | } 58 | _tiles[_height * y + x] = tile 59 | } 60 | 61 | width { _width } 62 | height { _height } 63 | } 64 | 65 | 66 | // Actions 67 | var ActorSquish = Fn.new {|actor| 68 | var world = actor.world 69 | for(i in 0...world.actors.count) { 70 | if (world.actors[i] == actor) { 71 | world.actors.removeAt(i) 72 | } 73 | } 74 | } 75 | 76 | var HaltX = Fn.new {|actor| 77 | actor.vel.x = 0 78 | actor.acc.x = 0 79 | } 80 | var HaltY = Fn.new {|actor| 81 | actor.vel.y = 0 82 | actor.acc.y = 0 83 | } 84 | 85 | // Engine classes 86 | class Entity { 87 | construct new(pos, size) { 88 | _pos = pos 89 | _size = size 90 | _world = null 91 | } 92 | 93 | construct new() { 94 | _pos = Vec.new() 95 | _size = Vec.new() 96 | } 97 | 98 | bindWorld(world) { 99 | _world = world 100 | } 101 | 102 | pos { _pos } 103 | pos=(v) { _pos = v } 104 | size { _size } 105 | size=(v) { _size = v } 106 | world { _world } 107 | 108 | update() {} 109 | draw(alpha) {} 110 | 111 | static isOverlapping(a, b) { 112 | return a.pos.x < b.pos.x + b.size.x && 113 | a.pos.x + a.size.x > b.pos.x && 114 | a.pos.y < b.pos.y + b.size.y && 115 | a.pos.y + a.size.y > b.pos.y 116 | } 117 | } 118 | 119 | class Actor is Entity { 120 | construct new(pos, size) { 121 | super(pos, size) 122 | _vel = Vec.new() 123 | _acc = Vec.new() 124 | _rx = 0 125 | _ry = 0 126 | } 127 | vel { _vel } 128 | vel=(v) { _vel = v } 129 | acc { _acc } 130 | acc=(v) { _acc = v } 131 | 132 | moveX(distance) { moveX(distance, null) } 133 | moveY(distance) { moveY(distance, null) } 134 | moveX(distance, action) { 135 | _rx = _rx + distance 136 | var move = Vec.new(M.round(_rx), 0) 137 | if (move.manhattan != 0) { 138 | _rx = _rx - move.x 139 | var sign = Vec.new(M.sign(move.x), 0) 140 | 141 | while (move.manhattan != 0) { 142 | var testActor = Actor.new(sign + pos, size) 143 | testActor.vel.x = sign.x 144 | if (!world.isColliding(this, testActor)) { 145 | pos = pos + sign 146 | move = move - sign 147 | } else { 148 | if (action != null) { 149 | action.call(this) 150 | } 151 | break 152 | } 153 | } 154 | } 155 | } 156 | moveY(distance, action) { 157 | _ry = _ry + distance 158 | var move = Vec.new(0, M.round(_ry)) 159 | if (move.manhattan != 0) { 160 | _ry = _ry - move.y 161 | var sign = Vec.new(0, M.sign(move.y)) 162 | 163 | while (move.manhattan != 0) { 164 | var testActor = Actor.new(sign + pos, size) 165 | if (!world.isColliding(this, testActor)) { 166 | move = move - sign 167 | pos = pos + sign 168 | } else { 169 | // if not collide 170 | if (action != null) { 171 | action.call(this) 172 | } 173 | break 174 | } 175 | } 176 | } 177 | } 178 | 179 | isAbove(solid) { 180 | return pos.y + size.y == solid.pos.y && 181 | pos.x < solid.pos.x + solid.size.x && 182 | pos.x + size.x > solid.pos.x 183 | } 184 | 185 | update() { 186 | moveY(vel.y, HaltY) 187 | moveX(vel.x, HaltX) 188 | } 189 | } 190 | 191 | class Solid is Entity { 192 | construct new(pos, size) { 193 | super(pos, size) 194 | _rx = 0 195 | _ry = 0 196 | _collidable = true 197 | _oneway = true 198 | } 199 | oneway { _oneway } 200 | collidable { _collidable } 201 | 202 | move(vec) { move(vec.x, vec.y) } 203 | move(x, y) { 204 | _rx = _rx + x 205 | _ry = _ry + y 206 | var mx = M.round(_rx) 207 | var my = M.round(_ry) 208 | if (mx != 0 || my != 0) { 209 | _collidable = false 210 | 211 | // x-axis movement first 212 | if (mx != 0) { 213 | var riding = getRiding() 214 | _rx = _rx - mx 215 | pos.x = pos.x + mx 216 | var right = pos.x + size.x 217 | var left = pos.x 218 | if (mx > 0) { 219 | world.actors.each {|actor| 220 | if (!oneway && Entity.isOverlapping(this, actor)) { 221 | var actorLeft = actor.pos.x 222 | actor.moveX(right - actorLeft, ActorSquish) 223 | } else if (riding.contains(actor)) { 224 | actor.moveX(mx) 225 | } 226 | } 227 | } else { 228 | world.actors.each {|actor| 229 | if (!oneway && Entity.isOverlapping(this, actor)) { 230 | var actorRight = actor.pos.x + actor.size.x 231 | actor.moveX(left - actorRight, ActorSquish) 232 | } else if (riding.contains(actor)) { 233 | actor.moveX(mx) 234 | } 235 | } 236 | } 237 | } 238 | if (my != 0) { 239 | var riding = getRiding() 240 | _ry = _ry - my 241 | pos.y = pos.y + my 242 | var bottom = pos.y + size.y 243 | var top = pos.y 244 | if (my > 0) { 245 | world.actors.each {|actor| 246 | if (Entity.isOverlapping(this, actor)) { 247 | var actorTop = actor.pos.y 248 | actor.moveY(bottom - actorTop, ActorSquish) 249 | } else if (riding.contains(actor)) { 250 | actor.moveY(my) 251 | } 252 | } 253 | } else { 254 | world.actors.each {|actor| 255 | if (Entity.isOverlapping(this, actor)) { 256 | var actorBottom = actor.pos.y + actor.size.y 257 | actor.moveY(top - actorBottom, ActorSquish) 258 | } else if (riding.contains(actor)) { 259 | actor.moveY(my) 260 | } 261 | } 262 | } 263 | } 264 | } 265 | _collidable = true 266 | } 267 | 268 | getRiding() { 269 | return world.actors.where {|actor| actor.isAbove(this) } 270 | } 271 | } 272 | 273 | // -------- GAME CODE --------- 274 | 275 | class Block is Solid { 276 | construct new(color, vel) { 277 | super( 278 | Vec.new(8 * TILE_SIZE, 13 * TILE_SIZE), 279 | Vec.new(2 * TILE_SIZE, 1 * TILE_SIZE) 280 | ) 281 | _vel = vel 282 | _color = color 283 | } 284 | 285 | construct new(pos, size, color, vel) { 286 | super(pos, size) 287 | _color = color 288 | _vel = vel 289 | } 290 | draw(alpha) { 291 | Canvas.rectfill(pos.x, pos.y, size.x, size.y, _color) 292 | } 293 | update() { 294 | if (pos.x < 8 * TILE_SIZE || pos.x >= 14 * TILE_SIZE) { 295 | _vel.x = -_vel.x 296 | } 297 | move(_vel) 298 | } 299 | } 300 | 301 | class Player is Actor { 302 | construct new() { 303 | super(Vec.new(), Vec.new(TILE_SIZE, TILE_SIZE)) 304 | _jumpButton = Key.new("space", true, false) 305 | } 306 | update() { 307 | if (Keyboard.isKeyDown("left")) { 308 | if (M.sign(vel.x) == 1) { 309 | acc.x = acc.x - CHANGE_FORCE 310 | } else { 311 | acc.x = acc.x - MOVE_FORCE 312 | } 313 | } else if (Keyboard.isKeyDown("right")) { 314 | if (M.sign(vel.x) == -1) { 315 | acc.x = acc.x + CHANGE_FORCE 316 | } else { 317 | acc.x = acc.x + MOVE_FORCE 318 | } 319 | } else { 320 | if (M.abs(vel.x) > MOVE_FORCE) { 321 | acc.x = -M.sign(vel.x) * FRICTION 322 | } else { 323 | acc.x = 0 324 | vel.x = 0 325 | } 326 | } 327 | 328 | var onGround = world.isOnGround(this) 329 | 330 | if (onGround) { 331 | if (_jumpButton.update()) { 332 | acc.y = -JUMP 333 | } 334 | var groundTiles = [ 335 | world.getTileAt(pos.x + size.x, pos.y + size.y), 336 | world.getTileAt(pos.x, pos.y + size.y) 337 | ] 338 | if (Keyboard.isKeyDown("down")) { 339 | var groundSolids = world.solids.where {|solid| isAbove(solid) } 340 | var fallthrough = false 341 | if (groundSolids.count > 0) { 342 | fallthrough = groundSolids.all {|solid| solid.collidable && solid.oneway } 343 | } else { 344 | fallthrough = groundTiles.all {|tile| (tile.type == 0 || tile.oneway) } 345 | } 346 | if (fallthrough) { 347 | pos.y = pos.y + 1 348 | onGround = world.isOnGround(this) 349 | } 350 | } 351 | } 352 | 353 | if (!onGround) { 354 | // This must not be 0.5, for rounding purposes 355 | acc.y = GRAVITY 356 | } else { 357 | // If this is enabled, you can't pass through tiles 358 | // Because it assumes you're on the ground now so you should stop 359 | // vel.y = 0 360 | } 361 | 362 | acc = acc + Vec.new() 363 | vel = vel + acc 364 | vel.x = M.mid(-MAX_SPEED, vel.x, MAX_SPEED) 365 | super.update() 366 | } 367 | draw(alpha) { 368 | Canvas.rectfill(pos.x, pos.y, size.x, size.y, Color.red) 369 | } 370 | } 371 | 372 | class World { 373 | construct init() { 374 | _solids = [Block.new(Color.orange, Vec.new(-0.1, 0))] 375 | _actors = [Player.new()] 376 | (_solids + _actors).each {|entity| entity.bindWorld(this) } 377 | _tilemap = BasicTilemap.init(16, 16) 378 | for (x in 0..._tilemap.width) { 379 | _tilemap.set(x, _tilemap.height - 1, Tile.new(1, true)) 380 | } 381 | _tilemap.set(4, 13, Tile.new(2, true, true)) 382 | } 383 | 384 | update() { 385 | _actors.each {|actor| actor.update() } 386 | _solids.each {|solid| solid.update() } 387 | } 388 | 389 | draw(alpha) { 390 | Canvas.cls(Color.blue) 391 | for (y in 0..._tilemap.height) { 392 | for (x in 0..._tilemap.width) { 393 | var tile = _tilemap.get(x, y) 394 | if (tile.type == 2) { 395 | Canvas.rectfill(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, Color.pink) 396 | } else if (tile.type == 1) { 397 | Canvas.rectfill(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, Color.darkgreen) 398 | } else { 399 | Canvas.rectfill(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, Color.new(0, 0, 0, 0)) 400 | } 401 | } 402 | } 403 | 404 | // Draw entities 405 | _solids.each {|solid| solid.draw(alpha) } 406 | _actors.each {|actor| actor.draw(alpha) } 407 | } 408 | 409 | getTileAt(vec) { getTileAt(vec.x, vec.y) } 410 | getTileAt(x, y) { 411 | var tx = M.floor(x / 8) 412 | var ty = M.floor(y / 8) 413 | return map.get(tx, ty) 414 | } 415 | isSolidAt(x, y) { 416 | var tx = M.floor(x / 8) 417 | var ty = M.floor(y / 8) 418 | return map.get(tx, ty).solid 419 | } 420 | 421 | isColliding(original, actor) { 422 | var colliding = false 423 | var pos = actor.pos 424 | var size = actor.size - Vec.new(1, 1) 425 | 426 | var tiles = [ 427 | pos, 428 | pos + size, 429 | Vec.new(pos.x, pos.y + size.y), 430 | Vec.new(pos.x + size.x, pos.y) 431 | ] 432 | 433 | var isSolid = false 434 | tiles.each { |tilePos| 435 | var tileTop = M.floor(tilePos.y / TILE_SIZE) * TILE_SIZE 436 | var tile = getTileAt(tilePos) 437 | if (tile.oneway && (original.pos + original.size).y > tileTop) { 438 | return 439 | } 440 | isSolid = isSolid || tile.solid 441 | } 442 | 443 | 444 | if (!isSolid) { 445 | solids.where {|solid| solid.collidable }.each {|solid| 446 | if (solid.oneway && (original.pos + original.size).y > solid.pos.y) { 447 | return 448 | } 449 | colliding = colliding || Entity.isOverlapping(actor, solid) 450 | } 451 | } 452 | 453 | return isSolid || colliding 454 | } 455 | 456 | isOnGround(actor) { 457 | var riding = false 458 | var pos = actor.pos 459 | var size = actor.size - Vec.new(1, 0) 460 | var tiles = [ 461 | pos + size, 462 | Vec.new(pos.x, pos.y + size.y) 463 | ] 464 | 465 | var solid = false 466 | tiles.each { |tilePos| 467 | var tileTop = M.floor(tilePos.y / TILE_SIZE) * TILE_SIZE 468 | var tile = getTileAt(tilePos) 469 | if (tile.oneway && (pos + size).y > tileTop) { 470 | return 471 | } 472 | solid = solid || tile.solid 473 | } 474 | if (!solid) { 475 | solids.where {|solid| solid.collidable }.each {|solid| 476 | if (solid.oneway && (pos + size).y > solid.pos.y) { 477 | return 478 | } 479 | riding = riding || actor.isAbove(solid) 480 | } 481 | } 482 | 483 | return solid || riding 484 | } 485 | 486 | map { _tilemap } 487 | actors { _actors } 488 | solids { _solids } 489 | } 490 | 491 | class Game { 492 | static init() { 493 | __world = World.init() 494 | Canvas.resize(128, 128) 495 | } 496 | static update() { 497 | if (Keyboard.isKeyDown("escape")) { 498 | Process.exit() 499 | } 500 | __world.update() 501 | } 502 | static draw(alpha) { 503 | __world.draw(alpha) 504 | } 505 | } 506 | -------------------------------------------------------------------------------- /roguelike/action.wren: -------------------------------------------------------------------------------- 1 | import "./dir" for Dir 2 | import "./events" for BoltEvent, EnergyDepletedEvent, MoveEvent 3 | 4 | class Action { 5 | type { _type } 6 | actor { _actor } 7 | game { _game } 8 | energy { 0 } 9 | 10 | construct none() { _type = "none" } 11 | construct new(type) { 12 | _type = type 13 | } 14 | 15 | bind(actor) { 16 | _game = actor.game 17 | _actor = actor 18 | } 19 | 20 | perform(result) { true } 21 | addEvent(event) { _game.addEventToResult(event) } 22 | } 23 | 24 | class DanceAction is Action { 25 | construct new() { 26 | super("dance") 27 | } 28 | perform(result) { 29 | System.print("%(actor.type) dances flagrantly!") 30 | return true 31 | } 32 | } 33 | class RestAction is Action { 34 | construct new() { 35 | super("rest") 36 | } 37 | perform(result) { 38 | System.print("%(actor.type) rests.") 39 | return true 40 | } 41 | } 42 | class TeleportAction is Action { 43 | construct new() { 44 | super("teleport") 45 | } 46 | perform(result) { 47 | System.print("You win!") 48 | return true 49 | } 50 | } 51 | 52 | 53 | class MoveAction is Action { 54 | construct new(direction) { 55 | super("move") 56 | _dir = direction 57 | } 58 | 59 | energy { _energy || 0 } 60 | 61 | perform(result) { 62 | System.print("Action(%(type)): %(actor.type)") 63 | if (_dir == null) { 64 | result.alternate = Action.none() 65 | return true 66 | } 67 | 68 | var destX = actor.x + Dir[_dir]["x"] 69 | var destY = actor.y + Dir[_dir]["y"] 70 | var validMove = false 71 | 72 | if (game.isTileValid(destX, destY)) { 73 | var tile = game.map.get(destX, destY) 74 | var isSolid = tile["solid"] 75 | var isOccupied = game.doesTileContainEntity(destX, destY) 76 | if (!isSolid && !isOccupied) { 77 | actor.x = destX 78 | actor.y = destY 79 | validMove = true 80 | if (tile["teleport"]) { 81 | result.alternate = TeleportAction.new() 82 | } 83 | } 84 | } 85 | return validMove 86 | } 87 | } 88 | 89 | class PlayerMoveAction is MoveAction { 90 | construct new(direction) { 91 | super(direction) 92 | } 93 | perform(result) { 94 | var validMove = super.perform(result) 95 | if (validMove) { 96 | addEvent(MoveEvent.new(actor, _dir)) 97 | } 98 | return validMove 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /roguelike/actor.wren: -------------------------------------------------------------------------------- 1 | import "./dir" for Dir 2 | import "./action" for Action, MoveAction, DanceAction 3 | import "math" for M 4 | 5 | var SLOWEST_SPEED = 0 6 | var SLOW_SPEED = 1 7 | var NORMAL_SPEED = 3 8 | var FAST_SPEED = 5 9 | 10 | var GAINS = [ 11 | 2, // third speed 12 | 3, // half speed 13 | 4, // 2/3rd speed 14 | 6, // Normal speed 15 | 9, 16 | 12, // double speed 17 | ] 18 | 19 | var THRESHOLD = 12 20 | 21 | class Actor { 22 | construct new(type, x, y) { 23 | _x = x 24 | _y = y 25 | _type = type 26 | _state = "ready" 27 | _energy = 0 28 | _speed = NORMAL_SPEED 29 | _visible = false 30 | } 31 | 32 | needsInput { false } 33 | // Energy Mechanics 34 | speed { _speed } 35 | speed=(v) { _speed = v } 36 | energy { _energy } 37 | gain() { 38 | if (type != "player") { 39 | System.print("%(this.type) gains %(GAINS[this.speed])") 40 | } 41 | _energy = _energy + GAINS[this.speed] 42 | return canTakeTurn 43 | } 44 | consume() { _energy = _energy % THRESHOLD } 45 | canTakeTurn { _energy >= THRESHOLD } 46 | // END energy mechanics 47 | 48 | visible { _visible } 49 | visible=(v) { _visible = v } 50 | 51 | x { _x } 52 | y { _y } 53 | x=(v) { _x = v } 54 | y=(v) { _y = v } 55 | type { _type } 56 | getAction() { Action.new(null) } 57 | 58 | state { _state } 59 | state=(s) { _state = s } 60 | 61 | bindGame(game) { _game = game } 62 | game { _game } 63 | } 64 | 65 | class Player is Actor { 66 | construct new(x, y) { 67 | super("player", x, y) 68 | visible = true 69 | _action = null 70 | } 71 | 72 | needsInput { _action == null } 73 | 74 | getAction() { 75 | var action = _action 76 | _action = null 77 | return action 78 | } 79 | action=(v) { _action = v } 80 | } 81 | 82 | class Blob is Actor { 83 | construct new(x, y) { 84 | super("blob", x, y) 85 | speed = SLOWEST_SPEED 86 | visible = true 87 | } 88 | getAction() { 89 | if (x > 0) { 90 | if (game.doesTileContainEntity(x - 1, y)) { 91 | return MoveAction.new(null) 92 | } 93 | return MoveAction.new("left") 94 | } else { 95 | return DanceAction.new() 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /roguelike/dir.wren: -------------------------------------------------------------------------------- 1 | import "math" for Vec 2 | var Dir = { 3 | "left": { "x": -1, "y": 0 }, 4 | "right": { "x": 1, "y": 0 }, 5 | "up": { "x": 0, "y": -1 }, 6 | "down": { "x": 0, "y": 1 } 7 | } 8 | -------------------------------------------------------------------------------- /roguelike/events.wren: -------------------------------------------------------------------------------- 1 | class Event {} 2 | 3 | class MoveEvent is Event { 4 | construct new(source, direction) { 5 | _source = source 6 | _direction = direction 7 | } 8 | source { _source } 9 | direction { _direction } 10 | 11 | } 12 | 13 | class GameOverEvent is Event { 14 | construct new() {} 15 | } 16 | class EnergyDepletedEvent is Event { 17 | construct new() {} 18 | } 19 | 20 | class BoltEvent is Event { 21 | source { _source } 22 | target { _target } 23 | construct new(source, tx, ty) { 24 | _source = source 25 | _target = [tx, ty] 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /roguelike/item.wren: -------------------------------------------------------------------------------- 1 | import "./action" for Action 2 | 3 | class ItemType { 4 | construct new(name) { 5 | _name = name 6 | } 7 | name { _name } 8 | } 9 | 10 | class Item { 11 | construct new(type) { 12 | _type = type 13 | } 14 | 15 | type { _type } 16 | getAction(type) { Action.none() } 17 | } 18 | 19 | // basic inventory with slots for every item 20 | class Inventory { 21 | construct init(size) { 22 | _items = [] 23 | if (size < 0) { 24 | Fiber.abort("Inventory size cannot be negative") 25 | } 26 | _size = size 27 | } 28 | construct init() { 29 | _items = [] 30 | _size = -1 31 | } 32 | 33 | add(item) { 34 | if (_size == -1 || _items.count < _size) { 35 | _items.add(item) 36 | } 37 | } 38 | 39 | clear() { 40 | _items.clear() 41 | } 42 | 43 | swap(index1, index2) { 44 | var temp = _items[index1] 45 | _items[index1] = _items[index2] 46 | _items[index2] = temp 47 | } 48 | 49 | remove(item) { 50 | for (index in 0..._items.count) { 51 | if (_items[index] == item) { 52 | return _items.removeAt(index) 53 | } 54 | } 55 | } 56 | } 57 | 58 | class ItemLocation { 59 | construct new(item, pos) { 60 | _item = item 61 | _pos = pos 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /roguelike/keys.wren: -------------------------------------------------------------------------------- 1 | import "input" for Keyboard, Mouse 2 | 3 | class InputGroup { 4 | construct new(inputs, action) { 5 | _inputs = inputs 6 | _action = action 7 | _firing = false 8 | } 9 | update() { 10 | _inputs.each {|input| input.update() } 11 | _firing = _inputs.count > 0 && _inputs.any {|input| input.firing } 12 | } 13 | 14 | firing { _firing } 15 | action { _action } 16 | } 17 | 18 | class DigitalInput { 19 | construct new(name, action, repeatable) { 20 | _name = name 21 | _repeatable = repeatable 22 | _action = action 23 | _counter = 0 24 | _wasPressed = false 25 | _firing = false 26 | } 27 | 28 | name { _name } 29 | firing { _firing } 30 | action { _action } 31 | 32 | update() { 33 | var isPressed = getInputState() 34 | var fire 35 | if (_repeatable) { 36 | fire = isPressed && (_counter == 0) 37 | if (!isPressed) { 38 | _counter = 0 39 | } else if (fire) { 40 | _counter = 8 41 | } else { 42 | _counter = _counter - 1 43 | } 44 | } else { 45 | fire = !_wasPressed && isPressed 46 | _wasPressed = isPressed 47 | } 48 | 49 | _firing = fire 50 | return fire 51 | } 52 | 53 | getInputState() { 54 | return false 55 | } 56 | } 57 | 58 | class Key is DigitalInput { 59 | construct new(name) { 60 | super(name, true, null) 61 | } 62 | construct new(name, repeatable) { 63 | super(name, repeatable, null) 64 | } 65 | construct new(name, repeatable, action) { 66 | super(name, repeatable, action) 67 | } 68 | 69 | getInputState() { 70 | return Keyboard.isKeyDown(name) 71 | } 72 | } 73 | 74 | class MouseButton is DigitalInput { 75 | construct new(key, result, repeat) { 76 | super(key, result, repeat) 77 | } 78 | construct new(key, result) { 79 | super(key, result, false) 80 | } 81 | 82 | getInputState() { 83 | return Mouse.isButtonPressed(name) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /roguelike/main.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for Canvas 2 | import "dome" for Window, Process 3 | import "input" for Keyboard 4 | import "./map" for TileMap, Tile 5 | import "./actor" for Player, Blob 6 | import "./model" for GameModel 7 | import "./view" for GameView 8 | 9 | class Game { 10 | 11 | static init() { 12 | var scale = 3 13 | Canvas.resize(128, 128) 14 | Window.resize(scale * 128, scale * 128) 15 | var map = TileMap.init(128, 128) 16 | map.set(3, 0, Tile.new(2, { "teleport": true })) 17 | for (x in 0...7) { 18 | map.set(x, 4, Tile.new(1, { "solid": true, "dark": false })) 19 | } 20 | var entities = [ 21 | Player.new(14, 6), 22 | Blob.new(14, 5) 23 | ] 24 | __view = GameView.init(GameModel.level(map, entities)) 25 | } 26 | 27 | static update() { 28 | if (Keyboard.isKeyDown("escape")) { 29 | Process.exit() 30 | } 31 | __view.update() 32 | } 33 | 34 | static draw(dt) { 35 | __view.draw() 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /roguelike/map.wren: -------------------------------------------------------------------------------- 1 | class Tile { 2 | construct new() { 3 | init_(0, {}) 4 | } 5 | construct new(type) { 6 | init_(type, {}) 7 | } 8 | construct new(type, data) { 9 | init_(type, data) 10 | } 11 | 12 | init_(type, data) { 13 | _type = type 14 | _data = data 15 | } 16 | 17 | type { _type } 18 | 19 | data { _data } 20 | [index] { _data[index] } 21 | [index]=(v) { _data[index] = v } 22 | } 23 | 24 | var EMPTY_TILE = Tile.new(0, { "dark": false }) 25 | 26 | class TileMap { 27 | construct init(width, height) { 28 | _width = width 29 | _height = height 30 | _tiles = List.filled(_width * _height, EMPTY_TILE) 31 | } 32 | 33 | clear(vec) { clear(vec.x, vec.y) } 34 | clear(x, y) { 35 | set(x, y, EMPTY_TILE) 36 | } 37 | get(vec) { get(vec.x, vec.y) } 38 | get(x, y) { 39 | if (x < 0 || x >= width || y < 0 || y >= height) { 40 | return Tile.new(-1, { "solid": true }) 41 | } 42 | return _tiles[_width * y + x] 43 | } 44 | 45 | set(vec, tile) { setTile(vec.x, vec.y, tile) } 46 | set(x, y, tile) { 47 | if (x < 0 || x >= width || y < 0 || y >= height) { 48 | Fiber.abort("Tile index out of bounds (%(x),%(y))") 49 | } 50 | if (!tile is Tile) { 51 | Fiber.abort("Only instances of Tile can be added to the tilemap") 52 | } 53 | _tiles[_width * y + x] = tile 54 | } 55 | 56 | width { _width } 57 | height { _height } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /roguelike/model.wren: -------------------------------------------------------------------------------- 1 | import "./dir" for Dir 2 | import "./action" for MoveAction 3 | import "./events" for BoltEvent, EnergyDepletedEvent 4 | import "./actor" for Player 5 | 6 | class GameResult { 7 | progress=(v) { _progress = v} 8 | progress { _progress } 9 | events { _events } 10 | alternate { _alternate } 11 | alternate=(v) { _alternate = v} 12 | 13 | construct new() { 14 | _progress = false 15 | _events = [] 16 | _alternate = null 17 | } 18 | } 19 | 20 | 21 | class GameModel { 22 | map { _map } 23 | player { _player } 24 | entities { _entities } 25 | energy { _entities[_turn].energy } 26 | turn { _turn } 27 | isPlayerTurn() { _entities[_turn] == _player } 28 | 29 | construct level(map, entities) { 30 | _map = map 31 | _entities = entities 32 | _entities.each{|entity| entity.bindGame(this) } 33 | _player = _entities.where {|entity| entity.type == "player" }.toList[0] 34 | _turn = 0 35 | } 36 | 37 | nextTurn() { 38 | _turn = (_turn + 1) % _entities.count 39 | } 40 | 41 | currentActor { _entities[_turn] } 42 | 43 | process() { 44 | var actor = currentActor 45 | if (actor.canTakeTurn && actor.needsInput) { 46 | return GameResult.new() 47 | } 48 | var action = null 49 | while (action == null) { 50 | actor = currentActor 51 | if (actor.canTakeTurn || actor.gain()) { 52 | if (actor.needsInput) { 53 | return GameResult.new() 54 | } 55 | action = actor.getAction() 56 | } else { 57 | nextTurn() 58 | } 59 | } 60 | action.bind(actor) 61 | _result = GameResult.new() 62 | _result.progress = action.perform(_result) 63 | while (_result.alternate != null) { 64 | action = _result.alternate 65 | _result.alternate = null 66 | action.bind(actor) 67 | _result.progress = action.perform(_result) 68 | } 69 | 70 | // Some actions should consume energy on failure 71 | if (_result.progress) { 72 | actor.consume() 73 | nextTurn() 74 | } 75 | 76 | return _result 77 | } 78 | 79 | isTileSolid(x, y) { 80 | return _map.get(x, y)["solid"] 81 | } 82 | 83 | isTileValid(x, y) { 84 | return (x >= 0 && x < map.width && y >= 0 && y < map.height) 85 | } 86 | 87 | doesTileContainEntity(x, y) { 88 | return _entities.any {|entity| entity.x == x && entity.y == y } 89 | } 90 | 91 | getEntitiesOnTile(x, y) { 92 | return _entities.where {|entity| entity.x == x && entity.y == y }.toList 93 | } 94 | 95 | addEventToResult(event) { 96 | if (_result != null) { 97 | _result.events.add(event) 98 | } else { 99 | Fiber.abort("Tried to add an event without a result") 100 | } 101 | } 102 | 103 | destroyEntity(entity) { 104 | _entities = _entities.where {|e| e != entity }.toList 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /roguelike/sprite.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for Canvas, ImageData, Color 2 | import "./main" for BulletAnimation 3 | 4 | class Sprite { 5 | construct new(imagePath, x, y) { 6 | _x = x 7 | _y = y 8 | _image = ImageData.loadFromFile(imagePath) 9 | } 10 | 11 | draw() { 12 | Canvas.draw(_image, _x*16+8, _y*16+8) 13 | } 14 | 15 | x { _x } 16 | y { _y } 17 | 18 | x=(value) { _x = value } 19 | y=(value) { _y = value } 20 | } 21 | -------------------------------------------------------------------------------- /roguelike/view.wren: -------------------------------------------------------------------------------- 1 | import "graphics" for Canvas, Color, ImageData 2 | import "input" for Keyboard 3 | import "math" for M, Vec 4 | import "./action" for PlayerMoveAction, DanceAction, RestAction 5 | import "./events" for GameOverEvent, MoveEvent 6 | import "./model" for GameModel 7 | import "./dir" for Dir 8 | import "./keys" for Key 9 | 10 | var Inputs = [ 11 | "left", 12 | "right", 13 | "up", 14 | "down" 15 | ].map {|key| Key.new(key, PlayerMoveAction.new(key), true) }.toList 16 | Inputs.add(Key.new("space", RestAction.new(), true)) 17 | Inputs.add(Key.new("d", DanceAction.new(), true)) 18 | 19 | 20 | 21 | 22 | var Angles = { 23 | "left": 90, 24 | "right": -90, 25 | "up": 180, 26 | "down": 0 27 | } 28 | 29 | class Animation { 30 | done=(v) { _done = v } 31 | done { _done || false } 32 | t { _t || 0 } 33 | update(view) { _t = t + 1 } 34 | draw() {} 35 | } 36 | class CameraAnimation is Animation { 37 | construct begin() { 38 | } 39 | update(view) { 40 | var player = view.model.player 41 | var camera = view.camera 42 | camera.x = camera.x - M.sign(camera.x - player.x) * 1/8 43 | camera.y = camera.y - M.sign(camera.y - player.y) * 1/8 44 | 45 | if (camera.x - player.x == 0 && camera.y - player.y == 0) { 46 | done = true 47 | } 48 | } 49 | } 50 | 51 | 52 | 53 | class GameView { 54 | construct init(gameModel) { 55 | _model = gameModel 56 | _events = [] 57 | _animations = [] 58 | _ready = true 59 | updateState() 60 | 61 | _camera = Vec.new(_model.player.x, _model.player.y) 62 | } 63 | camera { _camera } 64 | model { _model } 65 | 66 | update() { 67 | Inputs.each { |input| input.update() } 68 | if (_ready) { 69 | for (input in Inputs) { 70 | if (input.firing) { 71 | _model.player.action = input.action 72 | break 73 | } 74 | } 75 | var result = _model.process() 76 | _ready = _ready && !result.progress && result.events.count == 0 77 | _animations = processEvents(result.events) 78 | } else { 79 | _animations.each {|animation| animation.update(this) } 80 | 81 | _ready = _animations.count == 0 82 | if (_ready) { 83 | updateState() 84 | if (_gameOver) { 85 | } 86 | } 87 | } 88 | 89 | } 90 | 91 | draw() { 92 | Canvas.cls() 93 | var map = _currentMap 94 | if (_gameOver) { 95 | // TODO UI Stacking system 96 | Canvas.print("Game Over", 0, map.height * 8, Color.white) 97 | } else { 98 | Canvas.print("Player: %(_model.entities[0].energy)", 0,8, Color.white) 99 | Canvas.print("Blob: %(_model.entities[1].energy)", 0, 0, Color.white) 100 | } 101 | var player = _model.player 102 | 103 | 104 | var offX = (Canvas.width / 2) - (camera.x * 8) - 4 105 | var offY = (Canvas.height / 2) - (camera.y * 8) - 4 106 | 107 | var border = 8 108 | var minX = M.max(player.x - border, 0) 109 | var maxX = M.min(player.x + border, map.width) 110 | var minY = M.max(player.y - border, 0) 111 | var maxY = M.min(player.y + border, map.width) 112 | 113 | 114 | for (y in minY...maxY) { 115 | for (x in minX...maxX) { 116 | var tile = map.get(x, y) 117 | if (!tile["dark"]) { 118 | if (tile.type == 0) { 119 | Canvas.print(".", offX + x * 8, offY + y * 8, Color.darkgray) 120 | } else if (tile.type == 1) { 121 | Canvas.rectfill(offX + x * 8, offY + y * 8, 7, 8, Color.darkgray) 122 | } else if (tile.type == 2) { 123 | Canvas.print("*", offX + x * 8, offY + y * 8, Color.blue) 124 | } 125 | } 126 | } 127 | } 128 | 129 | _model.entities.each {|entity| 130 | if (!entity.visible) { 131 | return 132 | } 133 | if (entity.type == "player") { 134 | Canvas.rectfill(offX + 8 * camera.x, offY + 8*camera.y, 8, 8, Color.black) 135 | Canvas.print("@", offX + 8 * camera.x, offY + 8 * camera.y, Color.white) 136 | } else if (entity.type == "blob") { 137 | Canvas.print("s", offX + 8 * entity.x, offY + 8 * entity.y, Color.green) 138 | } 139 | } 140 | 141 | // Render one animation at a time 142 | if (_animations.count > 0) { 143 | var a = _animations[0] 144 | a.draw() 145 | if (a.done) { 146 | _animations.removeAt(0) 147 | } 148 | } 149 | } 150 | 151 | // Following the Redux model, you can up 152 | updateState() { 153 | _currentMap = _model.map 154 | _currentEnergy = _model.energy 155 | _gameOver = _gameOverImminent || false 156 | } 157 | 158 | // Respond to events generated by the Game Model since the last action was taken 159 | // You can trigger animations here and pass them back to the view 160 | processEvents(events) { 161 | return events.map {|event| 162 | if (event is GameOverEvent) { 163 | _gameOverImminent = true 164 | return null 165 | } else if (event is MoveEvent) { 166 | if (event.source == _model.player) { 167 | return CameraAnimation.begin() 168 | } 169 | } 170 | return null 171 | }.where {|animation| animation != null }.toList 172 | } 173 | } 174 | 175 | -------------------------------------------------------------------------------- /tileeditor/bg.csv: -------------------------------------------------------------------------------- 1 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 2 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 3 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 4 | 76,76,76,76,76,78,76,76,76,76,76,76,76,76,76,76 5 | 76,76,78,76,76,76,76,76,76,76,76,76,76,76,76,76 6 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,78,76 7 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 8 | 76,76,76,76,76,76,78,76,76,76,76,76,76,76,76,76 9 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 10 | 76,78,76,76,76,76,76,76,76,76,76,76,76,76,76,76 11 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 12 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 13 | 76,76,76,76,76,76,103,104,76,76,76,76,76,76,103,104 14 | 118,119,120,121,122,123,145,125,118,119,120,121,122,123,145,145 15 | 139,140,141,145,145,145,145,146,139,140,141,142,143,144,145,145 16 | -1,-1,-1,-1,-1,-1,145,-1,-1,-1,-1,-1,-1,-1,-1,-1 -------------------------------------------------------------------------------- /tileeditor/cute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/tileeditor/cute.png -------------------------------------------------------------------------------- /tileeditor/keys.wren: -------------------------------------------------------------------------------- 1 | import "input" for Keyboard, Mouse 2 | 3 | class Key { 4 | construct new(key, result) { 5 | _name = key 6 | _repeatable = true 7 | _wasPressed = false 8 | _result = result 9 | _counter = 0 10 | } 11 | construct new(key, result, repeatable) { 12 | _name = key 13 | _repeatable = repeatable 14 | _counter = 0 15 | _wasPressed = false 16 | _result = result 17 | } 18 | 19 | getButtonState() { 20 | return Keyboard.isKeyDown(name) 21 | } 22 | 23 | name { _name } 24 | result { _result } 25 | 26 | update() { 27 | var isPressed = getButtonState() 28 | var fire 29 | if (_repeatable) { 30 | fire = isPressed && (_counter == 0) 31 | if (!isPressed) { 32 | _counter = 0 33 | } else if (fire) { 34 | _counter = 8 35 | } else { 36 | _counter = _counter - 1 37 | } 38 | } else { 39 | fire = !_wasPressed && isPressed 40 | _wasPressed = isPressed 41 | } 42 | 43 | return fire 44 | } 45 | } 46 | 47 | class MouseButton is Key { 48 | construct new(key, result, repeat) { 49 | super(key, result, repeat) 50 | } 51 | construct new(key, result) { 52 | super(key, result, false) 53 | } 54 | 55 | getButtonState() { 56 | return Mouse.isButtonPressed(name) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tileeditor/level.map: -------------------------------------------------------------------------------- 1 | BACKGROUND #2E485B 2 | TILESET cute.png 3 | TILE 2 solid 4 | TILE 48 solid oneway 5 | TILE 63 solid oneway 6 | TILE 47 solid oneway 7 | TILE 66 solid oneway 8 | TILE 65 solid oneway 9 | TILE 89 hazard 10 | TILE 70 hazard 11 | TILE 88 hazard 12 | TILE 46 solid oneway 13 | TILE 64 solid oneway 14 | TILE 69 hazard 15 | DEFAULT 16 | TILE 45 solid oneway 17 | TILESET END 18 | LAYER 19 | SPRITESHEET tileset.png 20 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 21 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 22 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 23 | 76,76,76,76,76,78,76,76,76,76,76,76,76,76,76,76 24 | 76,76,78,76,76,76,76,76,76,76,76,76,76,76,76,76 25 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,78,76 26 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 27 | 76,76,76,76,76,76,78,76,76,76,76,76,76,76,76,76 28 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 29 | 76,78,76,76,76,76,76,76,76,76,76,76,76,76,76,76 30 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 31 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 32 | 76,76,76,76,76,76,103,104,76,76,76,76,76,76,103,104 33 | 118,119,120,121,122,123,145,125,118,119,120,121,122,123,145,145 34 | 139,140,141,145,145,145,145,146,139,140,141,142,143,144,145,145 35 | -1,-1,-1,-1,-1,-1,145,-1,-1,-1,-1,-1,139,139,139,139 36 | LAYER END 37 | LAYER 38 | SPRITESHEET tileset.png 39 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 40 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 41 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 42 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 43 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 44 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 45 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 46 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 47 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 48 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 49 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 50 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 51 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 52 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,145 53 | -1,-1,-1,-1,8,9,145,10,139,-1,-1,-1,143,-1,-1,145 54 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 55 | LAYER END 56 | LAYER 57 | SPRITESHEET cute.png 58 | SOLID 59 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 60 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 61 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 62 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 63 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 64 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 65 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 66 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 67 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 68 | -1,-1,-1,-1,-1,-1,-1,46,47,-1,-1,-1,-1,-1,-1,-1 69 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 70 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 71 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 72 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 73 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 74 | 2,2,2,2,2,2,2,2,2,2,2,2,89,89,89,89 75 | LAYER END 76 | LAYER 77 | SPRITESHEET tileset.png 78 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 79 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 80 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 81 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 82 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 83 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 84 | -1,-1,-1,-1,-1,-1,-1,-1,-1,88,89,90,-1,-1,-1,-1 85 | -1,-1,-1,-1,-1,-1,-1,-1,-1,109,110,111,-1,-1,-1,-1 86 | -1,-1,-1,-1,-1,-1,-1,-1,-1,130,131,132,-1,-1,-1,-1 87 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,112,-1,-1,-1,-1,-1 88 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 89 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 90 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 91 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 92 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,134,-1,-1,-1,-1,-1 93 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 94 | LAYER END -------------------------------------------------------------------------------- /tileeditor/level.map.bak: -------------------------------------------------------------------------------- 1 | BACKGROUND #2E485B 2 | 3 | TILESET cute.png 4 | TILE 2 solid 5 | TILE 45 solid oneway 6 | TILE 46 solid oneway 7 | TILE 47 solid oneway 8 | TILE 48 solid oneway 9 | 10 | TILE 63 solid oneway 11 | TILE 64 solid oneway 12 | TILE 65 solid oneway 13 | TILE 66 solid oneway 14 | TILESET END 15 | 16 | LAYER 17 | SPRITESHEET tileset.png 18 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 19 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 20 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 21 | 76,76,76,76,76,78,76,76,76,76,76,76,76,76,76,76 22 | 76,76,78,76,76,76,76,76,76,76,76,76,76,76,76,76 23 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,78,76 24 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 25 | 76,76,76,76,76,76,78,76,76,76,76,76,76,76,76,76 26 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 27 | 76,78,76,76,76,76,76,76,76,76,76,76,76,76,76,76 28 | 76,76,76,76,76,76,76,76,76,78,76,76,76,76,76,76 29 | 76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76 30 | 76,76,76,76,76,76,103,104,76,76,76,76,76,76,103,104 31 | 118,119,120,121,122,123,145,125,118,119,120,121,122,123,145,145 32 | 139,140,141,145,145,145,145,146,139,140,141,142,143,144,145,145 33 | -1,-1,-1,-1,-1,-1,145,-1,-1,-1,-1,-1,-1,-1,-1,-1 34 | LAYER END 35 | LAYER 36 | SPRITESHEET tileset.png 37 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 38 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 39 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 40 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 41 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 42 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 43 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 44 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 45 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 46 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 47 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 48 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 49 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 50 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,145 51 | -1,-1,-1,-1,8,9,145,10,139,-1,-1,-1,143,-1,-1,145 52 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 53 | LAYER END 54 | LAYER 55 | SPRITESHEET cute.png 56 | SOLID 57 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 58 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 59 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 60 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 61 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 62 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 63 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 64 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 65 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 66 | -1,-1,-1,-1,-1,-1,-1,46,47,-1,-1,-1,-1,-1,-1,-1 67 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 68 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 69 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 70 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 71 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 72 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 73 | LAYER END 74 | LAYER 75 | SPRITESHEET tileset.png 76 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 77 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 78 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 79 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 80 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 81 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 82 | -1,-1,-1,-1,-1,-1,-1,-1,-1,88,89,90,-1,-1,-1,-1 83 | -1,-1,-1,-1,-1,-1,-1,-1,-1,109,110,111,-1,-1,-1,-1 84 | -1,-1,-1,-1,-1,-1,-1,-1,-1,130,131,132,-1,-1,-1,-1 85 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,112,-1,-1,-1,-1,-1 86 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 87 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 88 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 89 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,133,-1,-1,-1,-1,-1 90 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,134,-1,-1,-1,-1,-1 91 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 92 | LAYER END 93 | -------------------------------------------------------------------------------- /tileeditor/main.wren: -------------------------------------------------------------------------------- 1 | import "dome" for Process, Window 2 | import "graphics" for Canvas, Color, ImageData 3 | import "input" for Keyboard, Mouse 4 | import "math" for Vec, M 5 | 6 | import "io" for FileSystem 7 | 8 | import "./keys" for Key, MouseButton 9 | import "./model" for Tile, BasicTileMap, Level 10 | 11 | // Physics Constants 12 | var JUMP = 3.1 13 | var GRAVITY = 0.2 14 | var FRICTION = 0.16 15 | 16 | var MAX_SPEED = 1 17 | var MOVE_FORCE = 0.08 18 | var CHANGE_FORCE = 0.5 19 | 20 | //World space 21 | var TILE_SIZE = 8 22 | 23 | class TileMapEditor { 24 | 25 | switchLayer(layer) { 26 | _layer = layer 27 | _spritesheet = _spritesheets[_layer] 28 | _sheetWidth = (_spritesheet.width / TILE_SIZE) 29 | _sheetHeight = (_spritesheet.height / TILE_SIZE) 30 | } 31 | 32 | construct init(level, spritesheets) { 33 | _level = level 34 | _maps = level.maps 35 | _layer = 0 36 | _spritesheets = spritesheets 37 | _renderers = List.filled(level.maps.count, null) 38 | for (layer in 0...level.maps.count) { 39 | _renderers[layer] = TileMapRenderer.init(_maps[layer], _spritesheets[layer]) 40 | } 41 | switchLayer(0) 42 | 43 | _mouseClick = MouseButton.new("left", true, true) 44 | _mouseReset = MouseButton.new("right", true, true) 45 | _next = Key.new("=", true, true) 46 | _prev = Key.new("-", true, true) 47 | _forward = Key.new("a", true, true) 48 | _back = Key.new("z", true, true) 49 | _save = Key.new("s", true, false) 50 | _selected = 0 51 | _offset = 0 52 | } 53 | 54 | update() { 55 | _tilemap = _maps[_layer] 56 | var x = M.floor(Mouse.x / TILE_SIZE) 57 | var y = M.floor(Mouse.y / TILE_SIZE) 58 | if (Keyboard.isKeyDown("left command") || 59 | Keyboard.isKeyDown("left shift")) { 60 | if (_forward.update()) { 61 | _layer = _layer + 1 62 | } 63 | if (_back.update()) { 64 | _layer = _layer - 1 65 | } 66 | for (btn in 1.._maps.count) { 67 | if (Keyboard.isKeyDown(btn.toString)) { 68 | _layer = btn - 1 69 | } 70 | } 71 | _layer = M.mid(0, _layer, _maps.count - 1) 72 | switchLayer(_layer) 73 | } 74 | if (Keyboard.isKeyDown("left command")) { 75 | var x = M.floor((Mouse.x - _offset) / TILE_SIZE) 76 | if (Mouse.isButtonPressed("left")) { 77 | if (x < _sheetWidth && y < _sheetHeight) { 78 | _selected = x + y * _sheetWidth 79 | } 80 | } 81 | if (_next.update()) { 82 | _offset = _offset - TILE_SIZE 83 | } 84 | if (_prev.update()) { 85 | _offset = _offset + TILE_SIZE 86 | } 87 | } else { 88 | if (_next.update()) { 89 | _selected = _selected + 1 90 | } 91 | if (_prev.update()) { 92 | _selected = _selected - 1 93 | } 94 | _selected = M.abs(_selected) 95 | 96 | if (Mouse.isButtonPressed("left")) { 97 | var type = _selected 98 | var sheetData = _level.properties[_level.spritesheets[_layer]] || {} 99 | var tileData = sheetData[type] || {} 100 | _tilemap.set(x, y, Tile.new(type, tileData)) 101 | } 102 | if (_mouseReset.update()) { 103 | var type = _tilemap.get(x, y).type 104 | _tilemap.clear(x, y) 105 | } 106 | } 107 | 108 | if (_save.update()) { 109 | _level.save() 110 | } 111 | } 112 | 113 | draw() { 114 | var x = M.floor(Mouse.x / TILE_SIZE) * TILE_SIZE 115 | var y = M.floor(Mouse.y / TILE_SIZE) * TILE_SIZE 116 | var tileX = (_selected % _sheetWidth).floor 117 | var tileY = (_selected / _sheetWidth).floor 118 | 119 | 120 | if (Keyboard.isKeyDown("left shift")) { 121 | Canvas.cls(_level.backgroundColor) 122 | _renderers[_layer].draw() 123 | } 124 | if (Keyboard.isKeyDown("left command")) { 125 | Canvas.rectfill(0, 0, _spritesheet.width, _spritesheet.height, Color.darkgray) 126 | _spritesheet.draw(_offset, 0) 127 | Canvas.rect(tileX * TILE_SIZE - 1 + _offset, tileY * TILE_SIZE -1 , TILE_SIZE+2, TILE_SIZE+2, Color.white) 128 | } else { 129 | _spritesheet.transform({ 130 | "srcX": (TILE_SIZE) * (tileX), 131 | "srcY": (TILE_SIZE) * (tileY), 132 | "srcW": TILE_SIZE, 133 | "srcH": TILE_SIZE 134 | }).draw(x, y) 135 | } 136 | if (Keyboard.isKeyDown("left command") || 137 | Keyboard.isKeyDown("left shift")) { 138 | Canvas.print((_layer + 1).toString, 0, 0, Color.white) 139 | Canvas.print(_selected, 0, _spritesheet.height, Color.white) 140 | } 141 | Canvas.rect(x-1, y-1, TILE_SIZE+2, TILE_SIZE+2, Color.red) 142 | } 143 | } 144 | 145 | class TileMapRenderer { 146 | construct init(tilemap, spritesheet) { 147 | _map = tilemap 148 | _spritesheet = spritesheet 149 | _sheetWidth = (_spritesheet.width / TILE_SIZE) 150 | _sheetHeight = (_spritesheet.height / TILE_SIZE) 151 | } 152 | 153 | draw() { 154 | for (y in 0..._map.height) { 155 | for (x in 0..._map.width) { 156 | var tile = _map.get(x, y).type 157 | if (tile != null) { 158 | var tileX = (tile % _sheetWidth).floor 159 | var tileY = (tile / _sheetWidth).floor 160 | _spritesheet.transform({ 161 | "srcX": (TILE_SIZE) * (tileX), 162 | "srcY": (TILE_SIZE) * (tileY), 163 | "srcW": TILE_SIZE, 164 | "srcH": TILE_SIZE 165 | }).draw(x * TILE_SIZE, y * TILE_SIZE) 166 | } 167 | } 168 | } 169 | } 170 | } 171 | 172 | // Actions 173 | var ActorSquish = Fn.new {|actor| 174 | var world = actor.world 175 | for(i in 0...world.actors.count) { 176 | if (world.actors[i] == actor) { 177 | world.actors.removeAt(i) 178 | } 179 | } 180 | } 181 | 182 | var HaltX = Fn.new {|actor| 183 | actor.vel.x = 0 184 | actor.acc.x = 0 185 | } 186 | var HaltY = Fn.new {|actor| 187 | actor.vel.y = 0 188 | actor.acc.y = 0 189 | } 190 | 191 | // Engine classes 192 | class Entity { 193 | construct new(pos, size) { 194 | _pos = pos 195 | _size = size 196 | _world = null 197 | } 198 | 199 | construct new() { 200 | _pos = Vec.new() 201 | _size = Vec.new() 202 | } 203 | 204 | bindWorld(world) { 205 | _world = world 206 | } 207 | 208 | pos { _pos } 209 | pos=(v) { _pos = v } 210 | size { _size } 211 | size=(v) { _size = v } 212 | world { _world } 213 | 214 | update() {} 215 | draw(alpha) {} 216 | 217 | static isOverlapping(a, b) { 218 | return a.pos.x < b.pos.x + b.size.x && 219 | a.pos.x + a.size.x > b.pos.x && 220 | a.pos.y < b.pos.y + b.size.y && 221 | a.pos.y + a.size.y > b.pos.y 222 | } 223 | } 224 | 225 | class Actor is Entity { 226 | construct new(pos, size) { 227 | super(pos, size) 228 | _vel = Vec.new() 229 | _acc = Vec.new() 230 | _rx = 0 231 | _ry = 0 232 | } 233 | vel { _vel } 234 | vel=(v) { _vel = v } 235 | acc { _acc } 236 | acc=(v) { _acc = v } 237 | 238 | moveX(distance) { moveX(distance, null) } 239 | moveY(distance) { moveY(distance, null) } 240 | moveX(distance, action) { 241 | _rx = _rx + distance 242 | var move = Vec.new(M.round(_rx), 0) 243 | if (move.manhattan != 0) { 244 | _rx = _rx - move.x 245 | var sign = Vec.new(M.sign(move.x), 0) 246 | 247 | while (move.manhattan != 0) { 248 | var testActor = Actor.new(sign + pos, size) 249 | testActor.vel.x = sign.x 250 | if (!world.isColliding(this, testActor)) { 251 | pos = pos + sign 252 | move = move - sign 253 | } else { 254 | if (action != null) { 255 | action.call(this) 256 | } 257 | break 258 | } 259 | } 260 | } 261 | } 262 | moveY(distance, action) { 263 | _ry = _ry + distance 264 | var move = Vec.new(0, M.round(_ry)) 265 | if (move.manhattan != 0) { 266 | _ry = _ry - move.y 267 | var sign = Vec.new(0, M.sign(move.y)) 268 | 269 | while (move.manhattan != 0) { 270 | var testActor = Actor.new(sign + pos, size) 271 | if (!world.isColliding(this, testActor)) { 272 | move = move - sign 273 | pos = pos + sign 274 | } else { 275 | // if not collide 276 | if (action != null) { 277 | action.call(this) 278 | } 279 | break 280 | } 281 | } 282 | } 283 | } 284 | 285 | isAbove(solid) { 286 | return pos.y + size.y == solid.pos.y && 287 | pos.x < solid.pos.x + solid.size.x && 288 | pos.x + size.x > solid.pos.x 289 | } 290 | 291 | update() { 292 | moveY(vel.y, HaltY) 293 | moveX(vel.x, HaltX) 294 | } 295 | } 296 | 297 | class Solid is Entity { 298 | construct new(pos, size) { 299 | super(pos, size) 300 | _rx = 0 301 | _ry = 0 302 | _data = { 303 | "collidable": true, 304 | "oneway": true 305 | } 306 | } 307 | data { _data } 308 | 309 | move(vec) { move(vec.x, vec.y) } 310 | move(x, y) { 311 | _rx = _rx + x 312 | _ry = _ry + y 313 | var mx = M.round(_rx) 314 | var my = M.round(_ry) 315 | if (mx != 0 || my != 0) { 316 | this["collidable"] = false 317 | 318 | // x-axis movement first 319 | if (mx != 0) { 320 | var riding = getRiding() 321 | _rx = _rx - mx 322 | pos.x = pos.x + mx 323 | var right = pos.x + size.x 324 | var left = pos.x 325 | if (mx > 0) { 326 | world.actors.each {|actor| 327 | if (!this["oneway"] && Entity.isOverlapping(this, actor)) { 328 | var actorLeft = actor.pos.x 329 | actor.moveX(right - actorLeft, ActorSquish) 330 | } else if (riding.contains(actor)) { 331 | actor.moveX(mx) 332 | } 333 | } 334 | } else { 335 | world.actors.each {|actor| 336 | if (!this["oneway"] && Entity.isOverlapping(this, actor)) { 337 | var actorRight = actor.pos.x + actor.size.x 338 | actor.moveX(left - actorRight, ActorSquish) 339 | } else if (riding.contains(actor)) { 340 | actor.moveX(mx) 341 | } 342 | } 343 | } 344 | } 345 | if (my != 0) { 346 | var riding = getRiding() 347 | _ry = _ry - my 348 | pos.y = pos.y + my 349 | var bottom = pos.y + size.y 350 | var top = pos.y 351 | if (my > 0) { 352 | world.actors.each {|actor| 353 | if (Entity.isOverlapping(this, actor)) { 354 | var actorTop = actor.pos.y 355 | actor.moveY(bottom - actorTop, ActorSquish) 356 | } else if (riding.contains(actor)) { 357 | actor.moveY(my) 358 | } 359 | } 360 | } else { 361 | world.actors.each {|actor| 362 | if (Entity.isOverlapping(this, actor)) { 363 | var actorBottom = actor.pos.y + actor.size.y 364 | actor.moveY(top - actorBottom, ActorSquish) 365 | } else if (riding.contains(actor)) { 366 | actor.moveY(my) 367 | } 368 | } 369 | } 370 | } 371 | } 372 | this["collidable"] = true 373 | } 374 | 375 | getRiding() { 376 | return world.actors.where {|actor| actor.isAbove(this) } 377 | } 378 | [index] { _data[index] || false } 379 | [index]=(v) { _data[index] = v } 380 | } 381 | 382 | // -------- GAME CODE --------- 383 | 384 | class Block is Solid { 385 | construct new(color, vel) { 386 | super( 387 | Vec.new(8 * TILE_SIZE, 12 * TILE_SIZE), 388 | Vec.new(2 * TILE_SIZE, 1 * TILE_SIZE) 389 | ) 390 | _vel = vel 391 | _color = color 392 | } 393 | 394 | construct new(pos, size, color, vel) { 395 | super(pos, size) 396 | _color = color 397 | _vel = vel 398 | } 399 | draw(alpha) { 400 | Canvas.rectfill(pos.x, pos.y, size.x, size.y, _color) 401 | } 402 | update() { 403 | if (pos.x < 8 * TILE_SIZE || pos.x >= 14 * TILE_SIZE) { 404 | _vel.x = -_vel.x 405 | } 406 | move(_vel) 407 | } 408 | } 409 | 410 | class Player is Actor { 411 | construct new() { 412 | super(Vec.new(), Vec.new(TILE_SIZE, TILE_SIZE)) 413 | _jumpButton = Key.new("space", true, false) 414 | } 415 | update() { 416 | if (Keyboard.isKeyDown("left")) { 417 | if (M.sign(vel.x) == 1) { 418 | acc.x = acc.x - CHANGE_FORCE 419 | } else { 420 | acc.x = acc.x - MOVE_FORCE 421 | } 422 | } else if (Keyboard.isKeyDown("right")) { 423 | if (M.sign(vel.x) == -1) { 424 | acc.x = acc.x + CHANGE_FORCE 425 | } else { 426 | acc.x = acc.x + MOVE_FORCE 427 | } 428 | } else { 429 | if (M.abs(vel.x) > MOVE_FORCE) { 430 | acc.x = -M.sign(vel.x) * FRICTION 431 | } else { 432 | acc.x = 0 433 | vel.x = 0 434 | } 435 | } 436 | 437 | var onGround = world.isOnGround(this) 438 | 439 | if (onGround) { 440 | if (_jumpButton.update()) { 441 | acc.y = -JUMP 442 | } 443 | var groundTiles = [ 444 | world.getTileAt(pos.x + size.x, pos.y + size.y), 445 | world.getTileAt(pos.x, pos.y + size.y) 446 | ] 447 | if (Keyboard.isKeyDown("down")) { 448 | var groundSolids = world.solids.where {|solid| isAbove(solid) } 449 | var fallthrough = false 450 | if (groundSolids.count > 0) { 451 | fallthrough = groundSolids.all {|solid| solid["collidable"] && solid["oneway"] } 452 | } else { 453 | fallthrough = groundTiles.all {|tile| (tile.type == null || tile["oneway"]) } 454 | } 455 | if (fallthrough) { 456 | pos.y = pos.y + 1 457 | onGround = world.isOnGround(this) 458 | } 459 | } 460 | } 461 | 462 | if (!onGround) { 463 | // This must not be 0.5, for rounding purposes 464 | acc.y = GRAVITY 465 | } else { 466 | // If this is enabled, you can't pass through oneway tiles 467 | // Because it assumes you're on the ground now so you should stop 468 | // vel.y = 0 469 | } 470 | 471 | acc = acc + Vec.new() 472 | vel = vel + acc 473 | vel.x = M.mid(-MAX_SPEED, vel.x, MAX_SPEED) 474 | super.update() 475 | 476 | // 477 | if (world.getOverlappingTiles(this).any {|tile| tile["hazard"] }) { 478 | ActorSquish.call(this) 479 | } 480 | } 481 | draw(alpha) { 482 | Canvas.rectfill(pos.x, pos.y, size.x, size.y, Color.red) 483 | } 484 | } 485 | 486 | class World { 487 | construct init(level) { 488 | _level = level 489 | _maps = level.maps 490 | 491 | _solidMapIndex = level.solidIndex 492 | _tilemap = _maps[_solidMapIndex] 493 | _solids = [Block.new(Color.orange, Vec.new(-0.1, 0))] 494 | _actors = [Player.new()] 495 | (_solids + _actors).each {|entity| entity.bindWorld(this) } 496 | 497 | _spritesheets = level.spritesheets.map {|name| ImageData.loadFromFile(name) }.toList 498 | _renderers = [] 499 | for (layer in 0...level.maps.count) { 500 | _renderers.add(TileMapRenderer.init(_maps[layer], _spritesheets[layer])) 501 | } 502 | _editor = TileMapEditor.init(level, _spritesheets) 503 | } 504 | 505 | update() { 506 | _actors.each {|actor| actor.update() } 507 | _solids.each {|solid| solid.update() } 508 | _editor.update() 509 | } 510 | 511 | draw(alpha) { 512 | Canvas.cls(_level.backgroundColor) 513 | for (layer in 0.._solidMapIndex) { 514 | _renderers[layer].draw() 515 | } 516 | 517 | // Draw entities 518 | _solids.each {|solid| solid.draw(alpha) } 519 | _actors.each {|actor| actor.draw(alpha) } 520 | for (layer in (_solidMapIndex+1)..._renderers.count) { 521 | _renderers[layer].draw() 522 | } 523 | 524 | _editor.draw() 525 | } 526 | 527 | getTileAt(vec) { getTileAt(vec.x, vec.y) } 528 | getTileAt(x, y) { 529 | var tx = M.floor(x / 8) 530 | var ty = M.floor(y / 8) 531 | return map.get(tx, ty) 532 | } 533 | isSolidAt(x, y) { 534 | return getTileAt(x, y)["solid"] 535 | } 536 | 537 | getOverlappingTiles(actor) { 538 | var pos = actor.pos 539 | var size = actor.size - Vec.new(1, 1) 540 | 541 | var tiles = [ 542 | pos, 543 | pos + size, 544 | Vec.new(pos.x, pos.y + size.y), 545 | Vec.new(pos.x + size.x, pos.y) 546 | ] 547 | 548 | return tiles.map { |tilePos| 549 | var tileTop = M.floor(tilePos.y / TILE_SIZE) * TILE_SIZE 550 | return getTileAt(tilePos) 551 | } 552 | } 553 | 554 | isColliding(original, actor) { 555 | var colliding = false 556 | var pos = actor.pos 557 | var size = actor.size - Vec.new(1, 1) 558 | 559 | var tiles = [ 560 | pos, 561 | pos + size, 562 | Vec.new(pos.x, pos.y + size.y), 563 | Vec.new(pos.x + size.x, pos.y) 564 | ] 565 | 566 | var isSolid = false 567 | tiles.each { |tilePos| 568 | var tileTop = M.floor(tilePos.y / TILE_SIZE) * TILE_SIZE 569 | var tile = getTileAt(tilePos) 570 | if (tile["oneway"] && (original.pos + original.size).y > tileTop) { 571 | return 572 | } 573 | isSolid = isSolid || tile["solid"] 574 | } 575 | 576 | 577 | if (!isSolid) { 578 | solids.where {|solid| solid["collidable"] }.each {|solid| 579 | if (solid["oneway"] && (original.pos + original.size).y > solid.pos.y) { 580 | return 581 | } 582 | colliding = colliding || Entity.isOverlapping(actor, solid) 583 | } 584 | } 585 | 586 | return isSolid || colliding 587 | } 588 | 589 | isOnGround(actor) { 590 | var riding = false 591 | var pos = actor.pos 592 | var size = actor.size - Vec.new(1, 0) 593 | var tiles = [ 594 | pos + size, 595 | Vec.new(pos.x, pos.y + size.y) 596 | ] 597 | 598 | var solid = false 599 | tiles.each { |tilePos| 600 | var tileTop = M.floor(tilePos.y / TILE_SIZE) * TILE_SIZE 601 | var tile = getTileAt(tilePos) 602 | if (tile["oneway"] && (pos + size).y > tileTop) { 603 | return 604 | } 605 | solid = solid || tile["solid"] 606 | } 607 | if (!solid) { 608 | solids.where {|solid| solid["collidable"] }.each {|solid| 609 | if (solid["oneway"] && (pos + size).y > solid.pos.y) { 610 | return 611 | } 612 | riding = riding || actor.isAbove(solid) 613 | } 614 | } 615 | 616 | return solid || riding 617 | } 618 | 619 | map { _tilemap } 620 | actors { _actors } 621 | solids { _solids } 622 | } 623 | 624 | class FilePicker { 625 | construct open(path) { 626 | navigate(path) 627 | _offset = 0 628 | _up = Key.new("up", true, true) 629 | _down = Key.new("down", true, true) 630 | _enter = Key.new("space", true, true) 631 | _pos = 0 632 | } 633 | 634 | navigate(path) { 635 | _path = path 636 | _directories = [".."] + FileSystem.listDirectories(path).skip(1).where {|name| !name.startsWith(".") }.toList 637 | _files = FileSystem.listFiles(path).where {|name| !name.startsWith(".") }.toList 638 | } 639 | 640 | update() { 641 | if (_up.update()) { 642 | _pos = _pos - 1 643 | } 644 | if (_down.update()) { 645 | _pos = _pos + 1 646 | } 647 | var items = (_directories + _files) 648 | _pos = M.min(items.count - 1, _pos) 649 | var selection = items[_pos] 650 | if (_enter.update()) { 651 | if (_directories.contains(selection)) { 652 | _path = _path + selection + "/" 653 | System.print(_path) 654 | navigate(_path) 655 | } 656 | } 657 | } 658 | 659 | draw(alpha) { 660 | Canvas.cls() 661 | var y = 0 662 | _directories.each {|name| 663 | if (_pos != y) { 664 | Canvas.print(name, 0, y * 8, Color.white) 665 | } else { 666 | Canvas.rectfill(0, y * 8, 128, 8, Color.white) 667 | Canvas.print(name, 0, y * 8, Color.black) 668 | } 669 | y = y + 1 670 | } 671 | _files.each {|name| 672 | if (_pos != y) { 673 | Canvas.print(name, 0, y * 8, Color.white) 674 | } else { 675 | Canvas.rectfill(0, y * 8, 128, 8, Color.white) 676 | Canvas.print(name, 0, y * 8, Color.black) 677 | } 678 | y = y + 1 679 | } 680 | } 681 | } 682 | 683 | class Game { 684 | static init() { 685 | Window.resize(4*128, 4*128) 686 | Canvas.resize(128, 128) 687 | Mouse.hidden = true 688 | var level = Level.fromFile("level.map") 689 | __world = World.init(level) 690 | __picker = FilePicker.open(FileSystem.basePath()) 691 | __state = __world 692 | } 693 | 694 | static update() { 695 | if (Keyboard.isKeyDown("escape")) { 696 | Process.exit() 697 | } 698 | __state.update() 699 | // __world.update() 700 | } 701 | static draw(alpha) { 702 | // __world.draw(alpha) 703 | __state.draw(alpha) 704 | } 705 | } 706 | -------------------------------------------------------------------------------- /tileeditor/map.csv: -------------------------------------------------------------------------------- 1 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 2 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 3 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 4 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 5 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 6 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 7 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 8 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 9 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 10 | -1,-1,-1,-1,-1,-1,-1,46,47,-1,-1,-1,-1,-1,-1,-1 11 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 12 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 13 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 14 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 15 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 16 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 -------------------------------------------------------------------------------- /tileeditor/map.csv.bak: -------------------------------------------------------------------------------- 1 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 2 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 3 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 4 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 5 | -1,-1,-1,-1,-1,-1,-1,-1,0,2,-1,-1,-1,-1,-1,-1 6 | -1,-1,-1,-1,-1,-1,-1,-1,21,23,-1,-1,-1,-1,-1,-1 7 | -1,-1,-1,-1,-1,-1,-1,-1,21,23,-1,-1,0,1,2,-1 8 | -1,-1,-1,-1,-1,-1,-1,0,85,23,-1,-1,-1,-1,-1,-1 9 | -1,-1,-1,0,2,-1,-1,21,22,23,-1,-1,-1,-1,-1,-1 10 | -1,-1,-1,42,44,-1,-1,21,22,23,1,1,-1,-1,-1,-1 11 | -1,-1,-1,-1,-1,-1,-1,21,22,23,-1,-1,-1,1,-1,-1 12 | -1,-1,-1,-1,-1,-1,0,85,22,23,-1,-1,-1,-1,-1,-1 13 | -1,-1,-1,1,1,-1,21,22,22,23,-1,-1,-1,-1,-1,-1 14 | -1,-1,-1,-1,-1,-1,21,22,22,23,-1,-1,-1,-1,-1,-1 15 | -1,-1,-1,-1,-1,-1,21,22,22,23,-1,-1,-1,-1,-1,-1 16 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 -------------------------------------------------------------------------------- /tileeditor/model.wren: -------------------------------------------------------------------------------- 1 | import "math" for M 2 | import "graphics" for Color 3 | import "io" for FileSystem 4 | 5 | var ERROR = -1 6 | var NONE = 0 7 | var LAYER = 1 8 | var TILESET = 2 9 | 10 | class Level { 11 | construct fromFile(filename) { 12 | _filename = filename 13 | var file = FileSystem.load(filename) 14 | var lines = file.split("\n") 15 | var mode = NONE 16 | 17 | _maps = [] 18 | _spritesheets = [] 19 | _solidIndex = 0 20 | _background = Color.black 21 | _tilesetProperties = {} 22 | 23 | var solid = false 24 | var mapWidth = 0 25 | var mapHeight = 0 26 | var tilesetName = null 27 | 28 | var layerSpritesheet = null 29 | var layerTileMap = [] 30 | 31 | lines.each {|line| 32 | var lineArray = line.trim().split(" ") 33 | if (mode == ERROR) { 34 | } else if (mode == TILESET) { 35 | if (line.trim() == "TILESET END") { 36 | mode = NONE 37 | } else if (lineArray[0].trim() == "TILE") { 38 | var index = Num.fromString(lineArray[1]) 39 | var tilesetData = _tilesetProperties[tilesetName] 40 | var data = {} 41 | 42 | for (prop in lineArray.skip(2)) { 43 | data[prop] = true 44 | } 45 | tilesetData[index] = data 46 | } else if (lineArray[0].trim() == "DEFAULT") { 47 | var index = -1 48 | var tilesetData = _tilesetProperties[tilesetName] 49 | var data = {} 50 | 51 | for (prop in lineArray.skip(1)) { 52 | data[prop] = true 53 | } 54 | tilesetData[index] = data 55 | } else { 56 | Fiber.abort("Invalid") 57 | } 58 | } else if (mode == NONE) { 59 | if (line.trim().count == 0) { 60 | return 61 | } else if (lineArray[0] == "TILESET") { 62 | mode = TILESET 63 | tilesetName = lineArray[1] 64 | _tilesetProperties[tilesetName] = {} 65 | } else if (lineArray[0] == "BACKGROUND") { 66 | _backgroundColor = Color.hex(lineArray[1]) 67 | } else if (line.trim() == "LAYER") { 68 | mode = LAYER 69 | solid = false 70 | mapWidth = 0 71 | mapHeight = 0 72 | layerSpritesheet = null 73 | layerTileMap = [] 74 | } else { 75 | Fiber.abort("Level file is invalid") 76 | } 77 | } else if (mode == LAYER) { 78 | if (line.trim() == "SOLID") { 79 | solid = true 80 | } else if (line.trim() == "LAYER END") { 81 | mode = NONE 82 | if (layerSpritesheet != null && layerTileMap.count > 0) { 83 | var tileMap = BasicTileMap.init(mapWidth, mapHeight) 84 | 85 | var tileData = _tilesetProperties[layerSpritesheet] || {} 86 | for (y in 0...mapHeight) { 87 | for (x in 0...mapWidth) { 88 | var pos = y * mapWidth + x 89 | var type = Num.fromString(layerTileMap[pos]) 90 | var data = tileData[type] || {} 91 | if (type < 0) { 92 | type = null 93 | } 94 | 95 | tileMap.set(x, y, Tile.new(type, data)) 96 | } 97 | } 98 | _maps.add(tileMap) 99 | if (solid) { 100 | _solidIndex = _maps.count - 1 101 | } 102 | _spritesheets.add(layerSpritesheet) 103 | } else { 104 | Fiber.abort("Level file is invalid") 105 | } 106 | } else { 107 | var row = line.trim() 108 | if (lineArray[0] == "SPRITESHEET") { 109 | layerSpritesheet = lineArray[1] 110 | } else if (row.count > 0) { 111 | // Assume it's fine 112 | var cols = row.split(",") 113 | mapWidth = M.max(mapWidth, cols.count) 114 | mapHeight = mapHeight + 1 115 | layerTileMap = layerTileMap + cols 116 | } 117 | } 118 | } else { 119 | Fiber.abort("Invalid") 120 | } 121 | } 122 | } 123 | 124 | save() { save(_filename) } 125 | save(filename) { 126 | var lines = [] 127 | var toHex = Fn.new {|dec| 128 | if (dec < 10) { 129 | return String.fromByte(dec + 48) 130 | } else if (dec < 16) { 131 | return String.fromByte((dec - 10) + 65) 132 | } 133 | } 134 | var rA = toHex.call(backgroundColor.r >> 4) 135 | var rB = toHex.call(backgroundColor.r & 15) 136 | var gA = toHex.call(backgroundColor.g >> 4) 137 | var gB = toHex.call(backgroundColor.g & 15) 138 | var bA = toHex.call(backgroundColor.b >> 4) 139 | var bB = toHex.call(backgroundColor.b & 15) 140 | lines.add("BACKGROUND #%(rA)%(rB)%(gA)%(gB)%(bA)%(bB)") 141 | for (key in properties.keys) { 142 | var data = properties[key] 143 | lines.add("TILESET %(key)") 144 | for (index in data.keys) { 145 | var props = data[index].keys.join(" ") 146 | if (index == -1) { 147 | lines.add("DEFAULT %(props)") 148 | } else { 149 | lines.add("TILE %(index) %(props)") 150 | } 151 | } 152 | lines.add("TILESET END") 153 | } 154 | 155 | for (layer in 0...maps.count) { 156 | var map = maps[layer] 157 | var spritesheet = spritesheets[layer] 158 | lines.add("LAYER") 159 | lines.add("SPRITESHEET %(spritesheet)") 160 | if (solidIndex == layer) { 161 | lines.add("SOLID") 162 | } 163 | 164 | for (y in 0...map.height) { 165 | var row = [] 166 | for (x in 0...map.width) { 167 | var type = map.get(x, y).type 168 | if (type == null) { 169 | type = -1 170 | } 171 | row.add(type) 172 | } 173 | lines.add(row.join(",")) 174 | } 175 | 176 | lines.add("LAYER END") 177 | } 178 | var err = Fiber.new { 179 | FileSystem.save(filename, lines.join("\n")) 180 | System.print("Saved!") 181 | }.try() 182 | if (err) { 183 | Fiber.abort(err) 184 | } 185 | } 186 | 187 | backgroundColor { _backgroundColor } 188 | solidIndex { _solidIndex } 189 | maps { _maps } 190 | spritesheets { _spritesheets } 191 | properties { _tilesetProperties } 192 | } 193 | 194 | class Tile { 195 | construct new() { 196 | init_(null, {}) 197 | } 198 | construct new(type) { 199 | init_(type, {}) 200 | } 201 | construct new(type, data) { 202 | init_(type, data) 203 | } 204 | 205 | init_(type, data) { 206 | _type = type 207 | _data = data 208 | } 209 | 210 | type { _type } 211 | 212 | data { _data } 213 | [index] { _data[index] } 214 | [index]=(v) { _data[index] = v } 215 | } 216 | 217 | var EMPTY_TILE = Tile.new() 218 | 219 | class BasicTileMap { 220 | construct init(width, height) { 221 | _width = width 222 | _height = height 223 | _tiles = List.filled(_width * _height, EMPTY_TILE) 224 | } 225 | 226 | clear(vec) { clear(vec.x, vec.y) } 227 | clear(x, y) { 228 | set(x, y, EMPTY_TILE) 229 | } 230 | get(vec) { get(vec.x, vec.y) } 231 | get(x, y) { 232 | if (x < 0 || x >= width || y < 0 || y >= height) { 233 | return Tile.new(null, { "solid": true }) 234 | } 235 | return _tiles[_width * y + x] 236 | } 237 | 238 | set(vec, tile) { setTile(vec.x, vec.y, tile) } 239 | set(x, y, tile) { 240 | if (x < 0 || x >= width || y < 0 || y >= height) { 241 | Fiber.abort("Tile index out of bounds (%(x),%(y))") 242 | } 243 | if (!tile is Tile) { 244 | Fiber.abort("Only instances of Tile can be added to the tilemap") 245 | } 246 | _tiles[_width * y + x] = tile 247 | } 248 | 249 | width { _width } 250 | height { _height } 251 | } 252 | 253 | -------------------------------------------------------------------------------- /tileeditor/tileset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/domeengine/dome-examples/8eb29810a4b3fdc7df79245ffce44cbc9e8dddde/tileeditor/tileset.png --------------------------------------------------------------------------------