public override void Update(float dt) { if (!isAttacking) { //Simple walk to player if (!walker.dest.HasValue || walker.dest != E.player.pos) { walker.SetDest(E.player.pos, 1, 0); } //Is we're blocked for some time try to find a way around other mobs if (walker.isWalkBlocked && walker.walkBlockedTime > 0.5f) //TODO: random time? { //log(format("%s %f", isWalkBlocked, walkBlockedTime)); walker.SetDest(E.player.pos, 1, 10); } //Attack if (!walker.isWalking && HexXY.Dist(pos, E.player.pos) == 1) { isAttacking = true; Interfacing.PerformInterfaceAttack(graphicsHandle, E.player.pos); fibered.StartFiber(AttackFiber()); } } base.Update(dt); }
void DoAfterMoveTo(Action action, HexXY p, uint dist) { if (HexXY.Dist(p, this.pos) <= dist) { action(); } else { afterMoveAction = action; afterMovePos = p; afterMoveDist = dist; dest = p; distToStop = 1; MovePart(); } }
public override void Update(float dt) { if (isWalking) { Walking(dt); } else { if (afterMoveAction != null && HexXY.Dist(afterMovePos, this.pos) <= afterMoveDist) { afterMoveAction(); afterMoveAction = null; } } //Mana regen mana = Mathf.Min(maxMana, mana + dt * 50); base.Update(dt); }
public SpellExecuting CastRanged(ICaster caster, HexXY target) { var pos = ((Entity)caster).pos; var dist = HexXY.Dist(pos, target); uint dir; if (dist == 0) { dir = ((Entity)caster).dir; } else { dir = HexXY.GetApproximateDir(pos, target); } var spEx = new SpellExecuting(caster, this, target, dir, false); spEx.SpawnAvatar(root, null, target, dir); executingSpells.Add(spEx); return(spEx); }
public static uint?FindPath(HexXY from, HexXY to, HexXY[] pathStorage, uint distToStop = 0, uint dynBlockCost = 0) { front.Reset(); front.Enqueue(new XYCost(from, 0, 0, GetHeuristic(from, to))); //TODO: assume we're in the single worldblock for now ++Level.S.pfExpandMarker; Level.S.SetPFExpandMap(from, Level.S.pfExpandMarker); XYCost c; bool isFound = false; do { c = front.Dequeue(); if (HexXY.Dist(c.p, to) <= distToStop) { isFound = true; break; } foreach (var st in steps) { var np = c.p + st; var blockType = Level.S.GetPFBlockedMap(np); if (WorldBlock.CanTryToMoveToBlockType(blockType) && Level.S.GetPFExpandMap(np) < Level.S.pfExpandMarker) { Level.S.SetPFExpandMap(np, Level.S.pfExpandMarker); uint cost = (uint)(c.cost + WorldBlock.PFGetPassCost(np)); if (dynBlockCost > 0 && np != to && blockType == WorldBlock.PFBlockType.DynamicBlocked) { cost += dynBlockCost; } var n = new XYCost(np, c.len + 1, cost, cost + GetHeuristic(np, to)); front.Enqueue(n); Level.S.SetPFStepsMap(np, st.backIdx); } } } while (front.Count > 0); if (isFound && c.len <= pathStorage.Length) { uint pathLen = c.len; HexXY p = c.p; for (int i = 0; i < pathLen; i++) { pathStorage[pathLen - i - 1] = p; var backIdx = Level.S.GetPFStepsMap(p); p = p + steps[backIdx]; } return(pathLen); } else { return(null); } }
static uint GetHeuristic(HexXY pos, HexXY to) { return(HexXY.Dist(pos, to)); }
void Walking(float dt) { HexXY prevPos = pos; float distToTargetSqr = (interFracDest - fracPos).sqrMagnitude; Vector2 dir = (interFracDest - fracPos).normalized; //Calculate blocking //Debug.Log(distToTargetSqr + " " + (blockForwardDist * blockForwardDist)); if (distToTargetSqr >= blockForwardDist * blockForwardDist) { Vector2 nextBlockPos = fracPos + dir * blockForwardDist; HexXY nextPosCell = HexXY.FromPlaneCoordinates(nextBlockPos); if (!blockedCells.Contains(nextPosCell)) { if (Level.S.GetPFBlockedMap(nextPosCell) != WorldBlock.PFBlockType.Unblocked) { //Blocked by something - stop and move to nearest cell center dest = interDest = pos; //Debug.Log("Blocked dest " + pos); interFracDest = pos.ToPlaneCoordinates(); float timeToGetThere = (fracPos - interFracDest).magnitude / (speed * 0.5f); //at half speed Interfacing.PerformInterfaceMovePrecise(graphicsHandle, interFracDest, timeToGetThere); return; } else { Level.S.SetPFBlockedMap(nextPosCell, WorldBlock.PFBlockType.DynamicBlocked); //G.S.DebugShowCell(nextPosCell); blockedCells.Add(nextPosCell); } } } //Calculate movement Vector2 step = dir * speed * dt; if (step.sqrMagnitude == 0 || step.sqrMagnitude > distToTargetSqr) { fracPos = interFracDest; if (HexXY.Dist(interDest, dest.Value) <= distToStop) { dest = null; isWalking = false; Interfacing.PerformInterfaceStop(graphicsHandle, pos); } else { MovePart(); } } else { fracPos += step; } pos = HexXY.FromPlaneCoordinates(fracPos); if (pos != prevPos) { Level.S.RemoveEntity(prevPos, this); Level.S.AddEntity(pos, this); //Remove blocked cells that are behind us blockedCellsToDelete.Clear(); foreach (var bc in blockedCells) { Vector2 bcFrac = bc.ToPlaneCoordinates(); if (bc != pos && Vector2.Dot(fracPos - bcFrac, dir) > blockBackDist) { blockedCellsToDelete.Add(bc); } } foreach (var dbc in blockedCellsToDelete) { blockedCells.Remove(dbc); Level.S.SetPFBlockedMap(dbc, WorldBlock.PFBlockType.Unblocked); //G.S.DebugHideCell(dbc); } } this.dir = HexXY.GetApproximateDir(dir); }