add collision with foreground

This commit is contained in:
Fabien Freling 2021-08-11 20:13:12 +02:00
parent 2ba5dd275f
commit c7fb1d5a86

View file

@ -8,6 +8,19 @@ import "random" for Random
var W = 240 var W = 240
var H = 136 var H = 136
var R = Random.new() var R = Random.new()
var TileSize = 8
var DbgBox = false
class Color {
static black { 0 }
static purple { 1 }
static red { 2 }
static orange { 3 }
static yellow { 4 }
static green_light { 5 }
static green { 6 }
static green_dark { 7 }
}
class Star { class Star {
construct new(x, y, size, scrollSpeed, color) { construct new(x, y, size, scrollSpeed, color) {
@ -90,15 +103,30 @@ class Enemy {
} }
} }
class Ship { class Player {
construct new(x, y) { construct new(x, y) {
_x = x _x = x
_y = y _y = y
_w = 16
_h = 16
// box size
_bw = 12
_bh = 10
_speed = 1 _speed = 1
_bullets = [] _bullets = []
_alive = true
} }
x { _x }
y { _y }
w { _w }
h { _h }
bw { _bw }
bh { _bh }
bullets { _bullets } bullets { _bullets }
alive { _alive }
update() { update() {
if (TIC.btn(0)) { if (TIC.btn(0)) {
@ -113,8 +141,8 @@ class Ship {
if (TIC.btn(3)) { if (TIC.btn(3)) {
_x = _x + _speed _x = _x + _speed
} }
_x.clamp(0, W) _x = _x.clamp(_w / 2, W - _w / 2)
_y.clamp(0, H) _y = _y.clamp(_h / 2, H - _h / 2)
if (TIC.btnp(4, 5, 10)) { if (TIC.btnp(4, 5, 10)) {
shoot() shoot()
@ -129,14 +157,22 @@ class Ship {
} }
draw() { draw() {
TIC.spr(256, _x, _y, 0, 1, 0, 0, 2, 2) TIC.spr(256, _x - _w / 2, _y - _h / 2, 0, 1, 0, 0, 2, 2)
for (b in _bullets) { for (b in _bullets) {
b.draw() b.draw()
} }
if (DbgBox) {
TIC.rectb(_x - _w / 2, _y - _h / 2, _w, _h, 5)
TIC.rectb(_x - _bw / 2, _y - _bh / 2, _bw, _bh, Color.red)
}
} }
shoot() { shoot() {
_bullets.add(Bullet.new(_x + 8, _y + 8)) _bullets.add(Bullet.new(_x + _w / 2, _y))
}
die() {
_alive = false
} }
} }
@ -157,7 +193,7 @@ class Explosion {
draw() { draw() {
var size = 10 var size = 10
TIC.circ(_x - size / 2, _y - size / 2, size, 2) TIC.circ(_x, _y, size, 2)
} }
} }
@ -167,9 +203,10 @@ class World {
_stars = [] _stars = []
_enemies = [] _enemies = []
_vfx = [] _vfx = []
_player = Ship.new(W / 2, H / 2) _player = Player.new(W / 2, H / 2)
_world_x = 0 _world_x = 0
_world_y = 0 _world_y = 0
_w_offset_x = 0
_world_scroll_speed = 60 / 2 _world_scroll_speed = 60 / 2
// init background // init background
@ -205,15 +242,18 @@ class World {
} }
update() { update() {
_player.update() if (_player.alive) {
_player.update()
for (s in _stars) {
s.update()
}
}
for (e in _enemies) { for (e in _enemies) {
e.update() e.update()
} }
for (s in _stars) {
s.update()
}
for (v in _vfx) { for (v in _vfx) {
v.update() v.update()
@ -239,11 +279,15 @@ class World {
if (_t % mediumTick == 0) { if (_t % mediumTick == 0) {
_stars.add(Star.new(W, R.int(0,H), width, speed, 13)) _stars.add(Star.new(W, R.int(0,H), width, speed, 13))
} }
_t = _t + 1
if (_player.alive) {
_t = _t + 1
}
if (_t % _world_scroll_speed == 0) { if (_t % _world_scroll_speed == 0) {
_world_x = _world_x + 1 _world_x = _world_x + 1
} }
_w_offset_x = -(_t % _world_scroll_speed) / (_world_scroll_speed / 8)
// Collisions // Collisions
var i = 0 var i = 0
@ -263,6 +307,14 @@ class World {
i = i + 1 i = i + 1
} }
} }
if (!(fgCheck(_player.x - _player.bw / 2, _player.y - _player.bh / 2) &&
fgCheck(_player.x - _player.bw / 2, _player.y + _player.bh / 2) &&
fgCheck(_player.x + _player.bw / 2, _player.y + _player.bh / 2) &&
fgCheck(_player.x + _player.bw / 2, _player.y - _player.bh / 2))) {
System.print("collision with foreground")
_player.die()
_vfx.add(Explosion.new(_player.x, _player.y))
}
} }
collide(x1, y1, w1, h1, x2, y2, w2, h2) { collide(x1, y1, w1, h1, x2, y2, w2, h2) {
@ -291,18 +343,28 @@ class World {
return true return true
} }
fgCheck(x, y) {
var mapX = ((x + _w_offset_x) / TileSize) + _world_x
var mapY = (y / TileSize) + _world_y
var tile = TIC.mget(mapX, mapY)
if (tile == 0) {
return true
}
System.print("collision at %(x), %(y)!")
return false
}
draw() { draw() {
for (s in _stars) { for (s in _stars) {
s.draw() s.draw()
} }
TIC.map(_world_x, _world_y, 30 + 1, 17 + 1, _w_offset_x, 0, 0, 1, _remap)
for (v in _vfx) { for (v in _vfx) {
v.draw() 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) { for (e in _enemies) {
e.draw() e.draw()
} }