textitem = nil projectile = nil projectileDistance = 200 projectileSpeed = 6000 tau = math.pi * 2 function removeObject(object) object:removeFromGame() end function removeText() removeObject(textitem) textitem = nil end function removeProjectile() removeObject(projectile) projectile = nil end -- pass 'nil' for angleOfAttack to attack from a random vector -- RADIANS! function kineticTrap(objectToHit, angleOfAttack) if(angleOfAttack == nil) then angleOfAttack = math.random(0, tau * 10) angleOfAttack = angleOfAttack * .1 end if(projectile == nil) then local currentPos = objectToHit:getLoc() local x = currentPos.x + (projectileDistance * math.cos(angleOfAttack)) local y = currentPos.y + (projectileDistance * math.sin(angleOfAttack)) projectile = ResourceItem.new() projectile:setLoc(x, y) projectile:setVel(point.new(-projectileSpeed * math.cos(angleOfAttack), -projectileSpeed * math.sin(angleOfAttack))) levelgen:addItem(projectile) Timer:scheduleOnce(removeProjectile, 4000) end end function textNearShip(ship, textMsg) if(textitem == nil) then local loc = ship:getLoc() textitem = TextItem.new() textitem:setText(textMsg) local textGeom = { point.new(loc.x - 150, loc.y + 25), point.new(loc.x + 150, loc.y + 25) } textitem:setGeom(textGeom) levelgen:addItem(textitem) Timer:scheduleOnce(removeText, 4000) end end function changeTeleportDest(teleporterId, dest) local teleporter = levelgen:findObjectById(teleporterId) if(teleporter ~= nil) then teleporter:clearDests() teleporter:addDest(dest) end end playersInWell = {} gravityWellCenter = levelgen:findObjectById(5):getLoc() gravityWellRadius = 255 -- roughly the radius of the zone gravityWellMaxVelocity = 600 -- equal to normal ship velocity -- This does a gravity well effect on any ship in the well. note that this -- only allows for one gravity well in the level function doGravityWellEffect() for i, v in ipairs(playersInWell) do local ship = v:getShip() local shipLoc = ship:getLoc() -- Find the vector and its distance from the ship to the center of the well local delta = point.new(shipLoc.x - gravityWellCenter.x, shipLoc.y - gravityWellCenter.y) local dist = point.length(delta) -- Calculate the pull based on how far from the center, pull harder (linearly) -- the closer you are (based on radius of well) -- local gravityPull = gravityWellMaxVelocity * (1 - (dist / gravityWellRadius)) -- quadratic x^2 -- local gravityPull = gravityWellMaxVelocity * (1 - ((dist * dist) / (gravityWellRadius * gravityWellRadius)) -- constant x^0 local gravityPull = gravityWellMaxVelocity * .333 local normalizedWellVel = point.normalize(delta) local wellVel = point.new(normalizedWellVel.x * gravityPull, normalizedWellVel.y * gravityPull) -- Now adjust the ship's velocity local shipVel = ship:getVel() ship:setVel(shipVel.x - wellVel.x, shipVel.y - wellVel.y) end end repairItem = levelgen:findObjectById(30) function testVision(ship) logprint(repairItem:isVis()) if(repairItem:isVis() == false) then local loc = ship:getLoc() resourceitem = ResourceItem.new() resourceitem:setLoc(loc) levelgen:addItem(resourceitem) end end function onShipEnteredZone(ship, zone, zoneType, zoneId) -- Do zone specific evilness if(zoneId == 6) then testVision(ship) elseif(zoneId == 5) then table.insert(playersInWell, ship:getPlayerInfo()) --ultimately handled in onTick() elseif(zoneId == 4) then local x = math.random(1, 1000) - 400 -- can't call random with negative numbers? local y = math.random(1, 1000) - 400 changeTeleportDest(10, point.new(x, y)) elseif(zoneId == 3) then kineticTrap(ship, nil) elseif(zoneId == 2) then textNearShip(ship, "Beware!") elseif(zoneId == 1) then textNearShip(ship, "Secret Zone!") end end function onShipLeftZone(ship, zone, zoneType, zoneId) if(zoneId == 5) then for i, v in ipairs(playersInWell) do if(v == ship:getPlayerInfo()) then table.remove(playersInWell, i) break end end end end function onTick(deltaTime) doGravityWellEffect() end -- main! function main() subscribe(Event.ShipEnteredZone) subscribe(Event.ShipLeftZone) subscribe(Event.Tick) end