refactor game state

master
Fabien Freling 2021-08-14 23:53:44 +02:00
parent d788900650
commit ac098d2310
1 changed files with 26 additions and 21 deletions

View File

@ -177,7 +177,7 @@ class Player {
_bw = 12 _bw = 12
_bh = 10 _bh = 10
_speed = 1 _speed = 2
_bullets = [] _bullets = []
_alive = true _alive = true
} }
@ -290,7 +290,7 @@ class World {
// init background // init background
for (i in 0..W) { for (i in 0..W) {
update(Game.title) update()
} }
_remap = Fn.new {|tile, x, y| _remap = Fn.new {|tile, x, y|
@ -318,14 +318,14 @@ class World {
TIC.mset(x, y, 0) TIC.mset(x, y, 0)
} }
update(state) { update() {
if (state == Game.pause) { if (Game.state == Game.pause) {
return return
} }
if (_player.alive) { if (_player.alive) {
if (state == Game.game) { if (Game.state == Game.game) {
_player.update() _player.update()
} }
@ -364,7 +364,7 @@ class World {
} }
// Map scrolling // Map scrolling
if (state == Game.game) { if (Game.state == Game.game) {
if (_t % _world_scroll_speed == 0) { if (_t % _world_scroll_speed == 0) {
_world_x = _world_x + 1 _world_x = _world_x + 1
} }
@ -417,6 +417,7 @@ class World {
if (collide(bx, by, _player.bw, _player.bh, e.x, e.y, e.w, e.h)) { if (collide(bx, by, _player.bw, _player.bh, e.x, e.y, e.w, e.h)) {
_player.die() _player.die()
_vfx.add(Explosion.new(_player.x, _player.y)) _vfx.add(Explosion.new(_player.x, _player.y))
Game.state = Game.gameover
} }
} }
} }
@ -459,7 +460,7 @@ class World {
return false return false
} }
draw(state) { draw() {
for (s in _stars) { for (s in _stars) {
s.draw() s.draw()
} }
@ -479,16 +480,18 @@ class World {
} }
class Game is TIC{ class Game is TIC{
state { _state } static state { __state }
static state=(s) { __state = s }
static title { "title" } static title { "title" }
static game { "game" } static game { "game" }
static pause { "pause" } static pause { "pause" }
static gameover { "gameover" }
construct new() { construct new() {
_t=0 _t=0
_x=96 _x=96
_y=24 _y=24
_state = Game.title __state = Game.title
_world = World.new() _world = World.new()
_p_buttons = [] _p_buttons = []
_p_index = 0 _p_index = 0
@ -502,21 +505,21 @@ class Game is TIC{
} }
update() { update() {
if (state == Game.title) { if (Game.state == Game.title) {
if (TIC.btnp(4)) { if (TIC.btnp(4)) {
_state = Game.game __state = Game.game
} }
} else if (state == Game.game) { } else if (Game.state == Game.game) {
if (TIC.btnp(5)) { if (TIC.btnp(5)) {
_state = Game.pause __state = Game.pause
_p_buttons = List.filled(10, -1) _p_buttons = List.filled(10, -1)
_p_index = 0 _p_index = 0
} }
} else if (state == Game.pause) { } else if (Game.state == Game.pause) {
if (TIC.btnp(5)) { if (TIC.btnp(5)) {
_state = Game.game __state = Game.game
} }
if (!Cheat.enabled) { if (!Cheat.enabled) {
@ -537,7 +540,7 @@ class Game is TIC{
} }
} }
_world.update(state) _world.update()
} }
cheatFound() { cheatFound() {
@ -558,17 +561,19 @@ class Game is TIC{
draw() { draw() {
TIC.cls(0) TIC.cls(0)
if (state == Game.title) { if (Game.state == Game.title) {
_world.draw(state) _world.draw()
TIC.print("Chi-Tor", W/4, H/4, Color.white, false, 3) TIC.print("Chi-Tor", W/4, H/4, Color.white, false, 3)
TIC.print("Chi-Tor", W/4-1, H/4-1, Color.red, false, 3) TIC.print("Chi-Tor", W/4-1, H/4-1, Color.red, false, 3)
TIC.print("Z", W/5, 4*H/5, Color.red) TIC.print("Z", W/5, 4*H/5, Color.red)
TIC.print(": shoot", W/5+8, 4*H/5, Color.white) TIC.print(": shoot", W/5+8, 4*H/5, Color.white)
TIC.print("X", W/5, 4*H/5+10, Color.red) TIC.print("X", W/5, 4*H/5+10, Color.red)
TIC.print(": pause", W/5+8, 4*H/5+10, Color.white) TIC.print(": pause", W/5+8, 4*H/5+10, Color.white)
} else if (state == Game.game) {
_world.draw(state) } else if (Game.state == Game.game) {
} else if (state == Game.pause) { _world.draw()
} else if (Game.state == Game.pause) {
TIC.print("PAUSE", 90, H/4, Color.white, false, 2) TIC.print("PAUSE", 90, H/4, Color.white, false, 2)
if (!Cheat.enabled) { if (!Cheat.enabled) {