From 2ba5dd275f61204ca1f3043f3b4ae430bda60bda Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Wed, 11 Aug 2021 18:55:14 +0200 Subject: [PATCH] add enemy --- cart.wren | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 6 deletions(-) diff --git a/cart.wren b/cart.wren index ddced7f..f5e1e7e 100644 --- a/cart.wren +++ b/cart.wren @@ -35,15 +35,22 @@ class Bullet { construct new(x, y) { _x = x _y = y + _w = 3 + _h = 1 _speed = 5 } + x { _x } + y { _y } + w { _w } + h { _h } + update() { _x = _x + _speed } draw() { - TIC.rect(_x, _y, 3, 1, 4) + TIC.rect(_x, _y, _w, _h, 4) } expired { @@ -51,6 +58,38 @@ class Bullet { } } +class Enemy { + construct new(x, y) { + _x = x + _y = y + _w = 8 + _h = 8 + _speed = 1 + _alive = true + } + + x { _x } + y { _y } + w { _w } + h { _h } + expired { !_alive } + + update() { + _x = _x - _speed + if (_x < -_w) { + _alive = false + } + } + + draw() { + TIC.spr(258, _x - _w / 2, _y - _h / 2, 0) + } + + hit() { + _alive = false + } +} + class Ship { construct new(x, y) { _x = x @@ -59,6 +98,8 @@ class Ship { _bullets = [] } + bullets { _bullets } + update() { if (TIC.btn(0)) { _y = _y - _speed @@ -99,10 +140,33 @@ class Ship { } } +class Explosion { + construct new(x, y) { + System.print("explosion at %(x), %(y)") + _x = x + _y = y + _countdown = 30 + } + + expired { _countdown < 0 } + + update() { + //System.print("explosion countdown %(_countdown)") + _countdown = _countdown - 1 + } + + draw() { + var size = 10 + TIC.circ(_x - size / 2, _y - size / 2, size, 2) + } +} + class World { construct new() { _t = 0 _stars = [] + _enemies = [] + _vfx = [] _player = Ship.new(W / 2, H / 2) _world_x = 0 _world_y = 0 @@ -112,18 +176,57 @@ class World { for (i in 0..W) { update() } + + _remap = Fn.new {|tile, x, y| + if (tile == 33) { + this.spawn(tile, x, y) + return 0 + } + return tile + } + } + + collect(list) { + var i = 0 + while (i < list.count) { + //System.print("%(list[i])") + if (list[i].expired) { + //System.print("remove element from list") + list.removeAt(i) + } else { + i = i + 1 + } + } + } + + spawn(tile, x, y) { + _enemies.add(Enemy.new(x * 4, y * 4)) + TIC.mset(x, y, 0) } update() { _player.update() + for (e in _enemies) { + e.update() + } + for (s in _stars) { s.update() } - // collect passed stars + + for (v in _vfx) { + v.update() + } + + // Collect while (_stars.count > 0 && _stars[0].expired) { _stars.removeAt(0) } + + collect(_enemies) + collect(_vfx) + // small stars if (_t % 30 == 0) { _stars.add(Star.new(W, R.int(0,H), 1, 1, 12)) @@ -141,14 +244,69 @@ class World { if (_t % _world_scroll_speed == 0) { _world_x = _world_x + 1 } + + // Collisions + var i = 0 + while (i < _player.bullets.count) { + var b = _player.bullets[i] + var touched = false + for (e in _enemies) { + if (collide(b.x, b.y, b.w, b.h, e.x, e.y, e.w, e.h)) { + _player.bullets.removeAt(i) + e.hit() + _vfx.add(Explosion.new(e.x + e.w / 2, e.y + e.h / 2)) + touched = true + break + } + } + if (!touched) { + i = i + 1 + } + } + } + + collide(x1, y1, w1, h1, x2, y2, w2, h2) { + // [x1] [x2] + if (x1 + w1 < x2) { + return false + } + + // [x2] [x1] + if (x1 > x2 + w2) { + return false + } + + // [y1] + // [y2] + if (y1 + h2 < y2) { + return false + } + + // [y2] + // [y1] + if (y1 > y2 + h2) { + return false + } + + return true } draw() { - var sx = (_t % _world_scroll_speed) / (_world_scroll_speed / 8) - TIC.map(_world_x, _world_y, 30 + 1, 17 + 1, -sx, 0) for (s in _stars) { s.draw() } + + for (v in _vfx) { + v.draw() + } + + var sx = (_t % _world_scroll_speed) / (_world_scroll_speed / 8) + TIC.map(_world_x, _world_y, 30 + 1, 17 + 1, -sx, 0, 0, 1, _remap) + + for (e in _enemies) { + e.draw() + } + _player.draw() } } @@ -179,8 +337,6 @@ class Game is TIC{ draw() { TIC.cls(0) _world.draw() - //TIC.spr(1+((_t%60)/30|0)*2,_x,_y,14,3,0,0,2,2) - //TIC.print("HELLO WORLD!",84,84) } } @@ -201,11 +357,13 @@ class Game is TIC{ // // 000:00000000000000000ccc000000eccccc00eeeeee4ddddddd4cccccdd43ddddcc // 001:00000000000000000000000000000000ee990000ddaa9900ddaaaa90ccaaaaa9 +// 002:2000000202333320032222300320023003200230032222300233332020000002 // 016:43dddddd4ddddddd4eeeeeee0ccccccc00000000000000000000000000000000 // 017:ddccccccddddddd0eeeeee000000000000000000000000000000000000000000 // // +// 006:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 014:000000000000000000000000000000000000000000000000000000000000000000000000005000000000000000500000000000000050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 015:000000000000000000000000000000000000000000000000000000000000000000000000505050000000000050505000000000005050500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 016:000000000000000000000000000000000000000000000000000000000000505050505050505050505050505050505050505050505050505050505050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000