--[[ This is the code we will implment for the basic Zombie we will need Zombie AI code as well for swarming and zombie code fo each of the different types From Main ZOMBIE_TYPE_BASIC = 1 ZOMBIE_TYPE_STRONGER = 2 ZOMBIE_TYPE_BIG = 3 ZOMBIE_TYPE_GIANT = 4 --]] --[[ Game space is 60x40 tiles We need to randomly spawn a zombie in this space. We need to check if the space is a valid tile. Otherwise the zombie will get stuck on the tile. We need a function to check x,y against a tilespace --]] HIT_ID = hash("contact_point_response") WALL_ID = hash("Wall") go.property("type", hash("ZOMBIE_TYPE_BASIC")) go.property("velocity", vmath.vector3()) function init(self) self.correction = vmath.vector3() msg.post("/spawner#spawner", "zombie_created") end function update(self, dt) --test AI for the moment --Why so slow? local startpos = go.get_position() local offset = vmath.vector3() --using 0 during debug mode offset.x = 0 --math.random(-32, 32) -- random target close by offset.y = 0 --math.random(-32, 32) -- random target close by local target =go.get_position("/player") + offset local dir = vmath.normalize(target - startpos) --we are trying to chase the player local p = go.get_position() + dir + self.velocity * dt go.set_position(p) --Make sure I face the right way sprite.set_hflip("#sprite", dir.x < 0) -- face the direction i', moving in --used for wall hits self.correction = vmath.vector3() end function on_message(self, message_id, message, sender) --[[ Zombie Collision Detection --]] -- Check if I hit a wall --if message_id == hash("contact_point_response") and message.group == hash("Wall") then if message_id == HIT_ID and message.group == WALL_ID then -- Get the info needed to move out of collision. We might -- get several contact points back and have to calculate -- how to move out of all of them by accumulating a -- correction vector for this frame: if message.distance > 0 then -- First, project the accumulated correction onto -- the penetration vector local proj = vmath.project(self.correction, message.normal * message.distance) if proj < 1 then -- Only care for projections that does not overshoot. local comp = (message.distance - message.distance * proj) * message.normal -- Apply compensation go.set_position(go.get_position() + comp) -- Accumulate correction done self.correction = self.correction + comp end end end end