move guns before ships
This commit is contained in:
		
							
								
								
									
										556
									
								
								updatedshmup.p8
									
									
									
									
									
								
							
							
						
						
									
										556
									
								
								updatedshmup.p8
									
									
									
									
									
								
							| @@ -548,6 +548,284 @@ function ship_m:refresh_shield() | ||||
|  self.shield_refresh_ready = lframe + self.shieldcooldown | ||||
| end | ||||
|  | ||||
| -->8 | ||||
| -- bullet and gun behaviors | ||||
|  | ||||
| bullet_base = { | ||||
|  enemyspd = 0.5 | ||||
| } | ||||
| mknew(bullet_base) | ||||
|  | ||||
| gun_base = {  | ||||
|  shoot_ready = -32768, | ||||
|  icon = 20 | ||||
| } | ||||
| mknew(gun_base) | ||||
|  | ||||
| function bullet_base:hitship(_) | ||||
|  self:die() | ||||
|  return true | ||||
| end | ||||
|  | ||||
| function bullet_base:die() | ||||
| end | ||||
|  | ||||
| function bullet_base:move() | ||||
|  self.x += self.dx | ||||
|  if self.enemy then | ||||
|   self.y += self.dy | ||||
|   if self.y > 128 then | ||||
|    self:die() | ||||
|    return true | ||||
|   end | ||||
|  else | ||||
|   self.y -= self.dy | ||||
|   if self.y < -8*self.height then | ||||
|    self:die() | ||||
|    return true | ||||
|   end | ||||
|  end | ||||
|  return false | ||||
| end | ||||
|  | ||||
| function bullet_base:draw() | ||||
|  spr(self.sprite, self.x, self.y, self.width, self.height) | ||||
| end | ||||
|  | ||||
| function bullet_base:spawn_at(x, y) | ||||
|  self.x = x - self.center_x_off | ||||
|  if self.enemy then | ||||
|   self.dx *= self.enemyspd | ||||
|   self.dy *= self.enemyspd | ||||
|   self.y = y + self.top_y_off | ||||
|   ebullets:push_back(self) | ||||
|  else | ||||
|   self.y = y - (8 * self.height) + self.bottom_y_off | ||||
|   pbullets:push_back(self) | ||||
|  end | ||||
| end | ||||
|  | ||||
| function gun_base:shoot(x, y) | ||||
|  if (lframe < self.shoot_ready) return false | ||||
|  if self.ammo then | ||||
|   if (self.ammo <= 0) return false | ||||
|   self.ammo -= 1 | ||||
|  end | ||||
|  self.shoot_ready = lframe + self.cooldown | ||||
|  self:actually_shoot(x, y) | ||||
|  return true | ||||
| end | ||||
|  | ||||
| function gun_base:actually_shoot(x, y) | ||||
|  local typ = self.t | ||||
|  local b = typ.new{ | ||||
|   enemy = self.enemy, | ||||
|   sprite = self.enemy and typ.esprite or typ.psprite, | ||||
|  } | ||||
|  b:spawn_at(x, y) | ||||
|  return true | ||||
| end | ||||
|  | ||||
| -->8 | ||||
| -- bullets and guns | ||||
|  | ||||
| zap = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 8, --index of player ammo sprite | ||||
|  esprite = 9, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 0, -- upper left corner | ||||
|   y_off = 0, -- relative to sprite | ||||
|   width = 2, | ||||
|   height = 8 | ||||
|  }, | ||||
|  center_x_off = 1, -- how to position by ship | ||||
|  bottom_y_off = 0, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 1, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 8, | ||||
|  | ||||
|  hitship = function(_, _) | ||||
|   return true | ||||
|  end | ||||
| } | ||||
| mknew(zap) | ||||
|  | ||||
| zap_gun = gun_base.new{ | ||||
|  enemy = false, | ||||
|  power = 20, -- power consumed per shot | ||||
|  cooldown = 0x0.000a, -- frames between shots | ||||
|  ammo = nil, -- unlimited ammo - main gun | ||||
|  t = zap -- metatable of bullet to fire | ||||
| } | ||||
| mknew(zap_gun) | ||||
|  | ||||
| blast = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 12, --index of player ammo sprite | ||||
|  esprite = 3, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 1, -- upper left corner | ||||
|   y_off = 1, -- relative to sprite | ||||
|   width = 6, | ||||
|   height = 6 | ||||
|  }, | ||||
|  center_x_off = 4, -- how to position by ship | ||||
|  bottom_y_off = 0, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 4, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 2, | ||||
|  awaitcancel = false, | ||||
|  | ||||
|  -- disable damage for 2 frames | ||||
|  -- when hitting something | ||||
|  hitship = function(self, _) | ||||
|   if self.damage > 0 and not self.awaitcancel then | ||||
|    self.awaitcancel = true | ||||
|    once_next_frame(function() | ||||
|     new_events:push_back{ | ||||
|      wait = 2, | ||||
|      obj = self, | ||||
|      saved_dmg = self.damage, | ||||
|      move = function(self) | ||||
|       self.wait -= 1 | ||||
|       if self.wait <= 0 then | ||||
|        self.obj.damage = self.saved_dmg | ||||
|        return true | ||||
|       end | ||||
|      end, | ||||
|     } | ||||
|     self.damage = 0 | ||||
|     self.awaitcancel = false | ||||
|    end) | ||||
|   end | ||||
|  end | ||||
| } | ||||
| mknew(blast) | ||||
|  | ||||
| blast_gun = gun_base.new{ | ||||
|  icon = 13, | ||||
|  enemy = false, | ||||
|  power = 0, -- ammo, not power | ||||
|  cooldown = 0x0.0020, -- frames between shots | ||||
|  ammo = 5, | ||||
|  maxammo = 5, | ||||
|  t = blast -- type of bullet to fire | ||||
| } | ||||
| mknew(blast_gun) | ||||
|  | ||||
| protron = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 23, --index of player ammo sprite | ||||
|  esprite = 24, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 1, -- upper left corner | ||||
|   y_off = 1, -- relative to sprite | ||||
|   width = 2, | ||||
|   height = 2 | ||||
|  }, | ||||
|  center_x_off = 1, -- how to position by ship | ||||
|  bottom_y_off = 4, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 1, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 3, | ||||
| } | ||||
| mknew(protron) | ||||
|  | ||||
| protron_gun = gun_base.new{ | ||||
|  icon = 25, | ||||
|  enemy = false, | ||||
|  power = 35, | ||||
|  cooldown = 0x0.000f, -- frames between shots | ||||
|  ammo = nil, | ||||
|  maxammo = nil, | ||||
|  actually_shoot = function(self, x, y) | ||||
|   local sprite = protron.psprite | ||||
|   if (self.enemy) sprite=protron.esprite | ||||
|   for i=1,3 do | ||||
|    local b = protron.new{ | ||||
|     enemy=self.enemy, | ||||
|     sprite=sprite, | ||||
|     dx = i, | ||||
|     dy = 4-i | ||||
|    } | ||||
|    b:spawn_at(x,y) | ||||
|    local b2 = protron.new{ | ||||
|     enemy=self.enemy, | ||||
|     sprite=sprite, | ||||
|     dx = -i, | ||||
|     dy = 4-i | ||||
|    } | ||||
|    b2:spawn_at(x,y) | ||||
|   end | ||||
|   local bup = protron.new{ | ||||
|    enemy=self.enemy, | ||||
|    sprite=sprite, | ||||
|    dy=4 | ||||
|   } | ||||
|   bup:spawn_at(x,y) | ||||
|  end | ||||
| } | ||||
| mknew(protron_gun) | ||||
|  | ||||
| vulcan = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 22, --index of player ammo sprite | ||||
|  esprite = 21, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 0, -- upper left corner | ||||
|   y_off = 0, -- relative to sprite | ||||
|   width = 1, | ||||
|   height = 4 | ||||
|  }, | ||||
|  center_x_off = 0.5, -- how to position by ship | ||||
|  bottom_y_off = 4, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 0.5, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 4, | ||||
| } | ||||
| mknew(vulcan) | ||||
|  | ||||
| vulcan_gun = gun_base.new{ | ||||
|  icon = 37, | ||||
|  enemy = false, | ||||
|  power = 8, | ||||
|  cooldown = 0x0.0002, -- frames between shots | ||||
|  ammo = nil, | ||||
|  maxammo = nil, | ||||
|  dxs = {0.35, -0.35, -0.7, 0.7, 0.35, -0.35}, | ||||
|  xoffs = {1, 0, -1, 1, 0, -1}, | ||||
|  dxidx = 1, | ||||
|  actually_shoot = function(self, x, y) | ||||
|   local sprite = self.enemy and vulcan.esprite or vulcan.psprite | ||||
|   local b = vulcan.new{ | ||||
|    enemy=self.enemy, | ||||
|    sprite=sprite, | ||||
|    dx = self.dxs[self.dxidx], | ||||
|   } | ||||
|   b:spawn_at(self.xoffs[self.dxidx]+x,y) | ||||
|   self.dxidx += 1 | ||||
|   if (self.dxidx > #self.dxs) self.dxidx = 1 | ||||
|  end | ||||
| } | ||||
| mknew(vulcan_gun) | ||||
|  | ||||
| -->8 | ||||
| --ships, including player | ||||
|  | ||||
| @@ -1091,284 +1369,6 @@ example_level_csv=[[1,spawn_frownie | ||||
| 700,spawn_blocking_boss_chasey | ||||
| 701,eol]] | ||||
|  | ||||
| -->8 | ||||
| -- bullet and gun behaviors | ||||
|  | ||||
| bullet_base = { | ||||
|  enemyspd = 0.5 | ||||
| } | ||||
| mknew(bullet_base) | ||||
|  | ||||
| gun_base = {  | ||||
|  shoot_ready = -32768, | ||||
|  icon = 20 | ||||
| } | ||||
| mknew(gun_base) | ||||
|  | ||||
| function bullet_base:hitship(_) | ||||
|  self:die() | ||||
|  return true | ||||
| end | ||||
|  | ||||
| function bullet_base:die() | ||||
| end | ||||
|  | ||||
| function bullet_base:move() | ||||
|  self.x += self.dx | ||||
|  if self.enemy then | ||||
|   self.y += self.dy | ||||
|   if self.y > 128 then | ||||
|    self:die() | ||||
|    return true | ||||
|   end | ||||
|  else | ||||
|   self.y -= self.dy | ||||
|   if self.y < -8*self.height then | ||||
|    self:die() | ||||
|    return true | ||||
|   end | ||||
|  end | ||||
|  return false | ||||
| end | ||||
|  | ||||
| function bullet_base:draw() | ||||
|  spr(self.sprite, self.x, self.y, self.width, self.height) | ||||
| end | ||||
|  | ||||
| function bullet_base:spawn_at(x, y) | ||||
|  self.x = x - self.center_x_off | ||||
|  if self.enemy then | ||||
|   self.dx *= self.enemyspd | ||||
|   self.dy *= self.enemyspd | ||||
|   self.y = y + self.top_y_off | ||||
|   ebullets:push_back(self) | ||||
|  else | ||||
|   self.y = y - (8 * self.height) + self.bottom_y_off | ||||
|   pbullets:push_back(self) | ||||
|  end | ||||
| end | ||||
|  | ||||
| function gun_base:shoot(x, y) | ||||
|  if (lframe < self.shoot_ready) return false | ||||
|  if self.ammo then | ||||
|   if (self.ammo <= 0) return false | ||||
|   self.ammo -= 1 | ||||
|  end | ||||
|  self.shoot_ready = lframe + self.cooldown | ||||
|  self:actually_shoot(x, y) | ||||
|  return true | ||||
| end | ||||
|  | ||||
| function gun_base:actually_shoot(x, y) | ||||
|  local typ = self.t | ||||
|  local b = typ.new{ | ||||
|   enemy = self.enemy, | ||||
|   sprite = self.enemy and typ.esprite or typ.psprite, | ||||
|  } | ||||
|  b:spawn_at(x, y) | ||||
|  return true | ||||
| end | ||||
|  | ||||
| -->8 | ||||
| -- bullets and guns | ||||
|  | ||||
| zap = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 8, --index of player ammo sprite | ||||
|  esprite = 9, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 0, -- upper left corner | ||||
|   y_off = 0, -- relative to sprite | ||||
|   width = 2, | ||||
|   height = 8 | ||||
|  }, | ||||
|  center_x_off = 1, -- how to position by ship | ||||
|  bottom_y_off = 0, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 1, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 8, | ||||
|  | ||||
|  hitship = function(_, _) | ||||
|   return true | ||||
|  end | ||||
| } | ||||
| mknew(zap) | ||||
|  | ||||
| zap_gun = gun_base.new{ | ||||
|  enemy = false, | ||||
|  power = 20, -- power consumed per shot | ||||
|  cooldown = 0x0.000a, -- frames between shots | ||||
|  ammo = nil, -- unlimited ammo - main gun | ||||
|  t = zap -- metatable of bullet to fire | ||||
| } | ||||
| mknew(zap_gun) | ||||
|  | ||||
| blast = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 12, --index of player ammo sprite | ||||
|  esprite = 3, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 1, -- upper left corner | ||||
|   y_off = 1, -- relative to sprite | ||||
|   width = 6, | ||||
|   height = 6 | ||||
|  }, | ||||
|  center_x_off = 4, -- how to position by ship | ||||
|  bottom_y_off = 0, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 4, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 2, | ||||
|  awaitcancel = false, | ||||
|  | ||||
|  -- disable damage for 2 frames | ||||
|  -- when hitting something | ||||
|  hitship = function(self, _) | ||||
|   if self.damage > 0 and not self.awaitcancel then | ||||
|    self.awaitcancel = true | ||||
|    once_next_frame(function() | ||||
|     new_events:push_back{ | ||||
|      wait = 2, | ||||
|      obj = self, | ||||
|      saved_dmg = self.damage, | ||||
|      move = function(self) | ||||
|       self.wait -= 1 | ||||
|       if self.wait <= 0 then | ||||
|        self.obj.damage = self.saved_dmg | ||||
|        return true | ||||
|       end | ||||
|      end, | ||||
|     } | ||||
|     self.damage = 0 | ||||
|     self.awaitcancel = false | ||||
|    end) | ||||
|   end | ||||
|  end | ||||
| } | ||||
| mknew(blast) | ||||
|  | ||||
| blast_gun = gun_base.new{ | ||||
|  icon = 13, | ||||
|  enemy = false, | ||||
|  power = 0, -- ammo, not power | ||||
|  cooldown = 0x0.0020, -- frames between shots | ||||
|  ammo = 5, | ||||
|  maxammo = 5, | ||||
|  t = blast -- type of bullet to fire | ||||
| } | ||||
| mknew(blast_gun) | ||||
|  | ||||
| protron = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 23, --index of player ammo sprite | ||||
|  esprite = 24, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 1, -- upper left corner | ||||
|   y_off = 1, -- relative to sprite | ||||
|   width = 2, | ||||
|   height = 2 | ||||
|  }, | ||||
|  center_x_off = 1, -- how to position by ship | ||||
|  bottom_y_off = 4, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 1, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 3, | ||||
| } | ||||
| mknew(protron) | ||||
|  | ||||
| protron_gun = gun_base.new{ | ||||
|  icon = 25, | ||||
|  enemy = false, | ||||
|  power = 35, | ||||
|  cooldown = 0x0.000f, -- frames between shots | ||||
|  ammo = nil, | ||||
|  maxammo = nil, | ||||
|  actually_shoot = function(self, x, y) | ||||
|   local sprite = protron.psprite | ||||
|   if (self.enemy) sprite=protron.esprite | ||||
|   for i=1,3 do | ||||
|    local b = protron.new{ | ||||
|     enemy=self.enemy, | ||||
|     sprite=sprite, | ||||
|     dx = i, | ||||
|     dy = 4-i | ||||
|    } | ||||
|    b:spawn_at(x,y) | ||||
|    local b2 = protron.new{ | ||||
|     enemy=self.enemy, | ||||
|     sprite=sprite, | ||||
|     dx = -i, | ||||
|     dy = 4-i | ||||
|    } | ||||
|    b2:spawn_at(x,y) | ||||
|   end | ||||
|   local bup = protron.new{ | ||||
|    enemy=self.enemy, | ||||
|    sprite=sprite, | ||||
|    dy=4 | ||||
|   } | ||||
|   bup:spawn_at(x,y) | ||||
|  end | ||||
| } | ||||
| mknew(protron_gun) | ||||
|  | ||||
| vulcan = bullet_base.new{ | ||||
|  --shape | ||||
|  psprite = 22, --index of player ammo sprite | ||||
|  esprite = 21, -- index of enemy ammo sprite | ||||
|  width = 1, --in 8x8 blocks | ||||
|  height = 1, | ||||
|  hurt = { -- hurtbox - where this ship can be hit | ||||
|   x_off = 0, -- upper left corner | ||||
|   y_off = 0, -- relative to sprite | ||||
|   width = 1, | ||||
|   height = 4 | ||||
|  }, | ||||
|  center_x_off = 0.5, -- how to position by ship | ||||
|  bottom_y_off = 4, | ||||
|  top_y_off = 0, | ||||
|   | ||||
|  damage = 0.5, | ||||
|  dx = 0,  -- px/frame | ||||
|  dy = 4, | ||||
| } | ||||
| mknew(vulcan) | ||||
|  | ||||
| vulcan_gun = gun_base.new{ | ||||
|  icon = 37, | ||||
|  enemy = false, | ||||
|  power = 8, | ||||
|  cooldown = 0x0.0002, -- frames between shots | ||||
|  ammo = nil, | ||||
|  maxammo = nil, | ||||
|  dxs = {0.35, -0.35, -0.7, 0.7, 0.35, -0.35}, | ||||
|  xoffs = {1, 0, -1, 1, 0, -1}, | ||||
|  dxidx = 1, | ||||
|  actually_shoot = function(self, x, y) | ||||
|   local sprite = self.enemy and vulcan.esprite or vulcan.psprite | ||||
|   local b = vulcan.new{ | ||||
|    enemy=self.enemy, | ||||
|    sprite=sprite, | ||||
|    dx = self.dxs[self.dxidx], | ||||
|   } | ||||
|   b:spawn_at(self.xoffs[self.dxidx]+x,y) | ||||
|   self.dxidx += 1 | ||||
|   if (self.dxidx > #self.dxs) self.dxidx = 1 | ||||
|  end | ||||
| } | ||||
| mknew(vulcan_gun) | ||||
|  | ||||
| -->8 | ||||
| -- readme.md | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user