diff --git a/src/Map.lua b/src/Map.lua index 8fa4d33c..c92ecc5b 100644 --- a/src/Map.lua +++ b/src/Map.lua @@ -33,6 +33,10 @@ function Map:update(dt) for i, path in ipairs(self.paths.objects) do path:update(dt) end end +function Map:getPathID(path) + for i, pathT in ipairs(self.paths.objects) do if pathT == path then return i end end +end + function Map:draw() diff --git a/src/ShotSphere.lua b/src/ShotSphere.lua index 469ffffc..8e316c10 100644 --- a/src/ShotSphere.lua +++ b/src/ShotSphere.lua @@ -88,10 +88,36 @@ function ShotSphere:moveStep() end end +function ShotSphere:getHitSphereIDs() + if not self.hitSphere then + return nil + end + + local s = self.hitSphere + local g = s.sphereGroup + local c = g.sphereChain + local p = c.path + local m = p.map + + local sphereID = s.sphereID + local groupID = c:getSphereGroupID(g) + local chainID = p:getSphereChainID(c) + local pathID = m:getPathID(p) + + return { + sphereID = sphereID, + groupID = groupID, + chainID = chainID, + pathID = pathID + } +end + function ShotSphere:destroy() if self.delQueue then return end self._list:destroy(self) - self.sphereEntity:destroy(false) + if self.sphereEntity then + self.sphereEntity:destroy(false) + end self.delQueue = true self.shooter:activate() end @@ -130,12 +156,7 @@ function ShotSphere:serialize() color = self.color, speed = self.speed, steps = self.steps, - hitSphere = { - pathID = 1, - chainID = 2, - groupID = 1, - sphereID = 8 - }, -- TODO: add an indexation function in Sphere.lua to resolve this problem + hitSphere = self:getHitSphereIDs(), hitTime = self.hitTime } end @@ -148,12 +169,20 @@ function ShotSphere:deserialize(t) self.shooter = game.session.level.shooter - self.hitSphere = nil -- blah blah blah, see above - self.hitTime = t.hitTime - + self.hitSphere = nil + self.sphereEntity = nil + if t.hitSphere then + self.hitSphere = { + sphereID = t.hitSphere.sphereID, + sphereGroup = game.session.level.map.paths.objects[t.hitSphere.pathID].sphereChains[t.hitSphere.chainID].sphereGroups[t.hitSphere.groupID] + } + else + self.sphereEntity = SphereEntity(self.pos, self.color) + self.sphereEntity.frame = Vec2(1) + end - self.sphereEntity = SphereEntity(self.pos, self.color) + self.hitTime = t.hitTime end return ShotSphere diff --git a/src/SphereGroup.lua b/src/SphereGroup.lua index a802d9f5..f2a8c0b8 100644 --- a/src/SphereGroup.lua +++ b/src/SphereGroup.lua @@ -612,15 +612,17 @@ function SphereGroup:deserialize(t) self.offset = t.offset self.speed = t.speed self.spheres = {} + local offset = 0 for i, sphere in ipairs(t.spheres) do local s = Sphere(self, sphere) - s.offset = (i - 1) * 32 + s.offset = offset -- links are mandatory!!! if i > 1 then s.prevSphere = self.spheres[i - 1] self.spheres[i - 1].nextSphere = s end table.insert(self.spheres, s) + offset = offset + 32 * s.size end self.matchCheck = t.matchCheck end