Beispiel #1
0
        public void update(TileMap map, TimeSpan elapsed)
        {
            if (alive) {
                lastElapsed = elapsed;

                if (horizontal) {
                    drawRect.X += getRealSpeed();
                    collRect.X += getRealSpeed();
                } else {
                    drawRect.Y += getRealSpeed();
                    collRect.Y += getRealSpeed();
                }

                distTraveled += Math.Abs(getRealSpeed());

                if (distTraveled > maxdist || drawRect.Right < 0 || drawRect.Left >= map.getPixelWidth()) {
                    alive = false;
                } else {
                    // Test walls based on direction (left, right)
                    if (horizontal) {
                        int x;
                        if (msSpeed > 0) x = map.shrinkX(collRect.Right, false);
                        else x = map.shrinkX(collRect.Left, false);

                        int maxY = map.shrinkY(collRect.Bottom, true);
                        for (int y = map.shrinkY(collRect.Top, false); y <= maxY; y++) {
                            Rectangle wallRect = map.getRect(x, y);

                            if (map.checkCollision(collRect, wallRect)) {
                                alive = false;
                                return;
                            }
                        }
                    } else {
                        int y;
                        if (msSpeed > 0) y = map.shrinkX(collRect.Bottom, false);
                        else y = map.shrinkX(collRect.Top, false);

                        int maxX = map.shrinkX(collRect.Left, true);
                        for (int x = map.shrinkX(collRect.Right, false); x <= maxX; x++) {
                            Rectangle wallRect = map.getRect(x, y);

                            if (map.checkCollision(collRect, wallRect)) {
                                alive = false;
                                return;
                            }
                        }
                    }

                    // Test collision with entites
                    foreach (Entity e in map.entityIterator()) {
                        if (!e.Alive)
                            continue;

                        EntityHit eHit;
                        if (horizontal) {
                            eHit = e.EBounds.collide(new Point(collRect.Right, collRect.Center.Y));
                            // If not hit in front, check back
                            if (eHit.Part == EntityPart.None)
                                eHit = e.EBounds.collide(new Point(collRect.Left, collRect.Center.Y));
                        } else {
                            int y;
                            if (msSpeed > 0) y = collRect.Bottom;
                            else y = collRect.Top;

                            eHit = e.EBounds.collide(new Point(collRect.Left, y));
                            // If not hit on left, check right
                            if (eHit.Part == EntityPart.None)
                                eHit = e.EBounds.collide(new Point(collRect.Right, y));
                        }

                        if (eHit.Part != EntityPart.None) {
                            alive = false;
                            if (eHit.Part != EntityPart.Miss) {
                                float dmgReducer = ((eHit.PercFromCenter < 0.6) ? 1 - eHit.PercFromCenter : 0.4f);
                                int realDmg = e.hitInThe(eHit.Part, dmg, dmgReducer);
                                map.addHitText(e, realDmg);
                                if (!e.Alive)
                                    xp += e.XPValue;
                            }
                            return;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void update(TileMap map, TimeSpan elapsed)
        {
            if (alive)
            {
                lastElapsed = elapsed;

                int vel = getRealSpeed();
                if (horizontal)
                {
                    drawRect.X += vel;
                    bounds.moveX(vel);
                }
                else
                {
                    drawRect.Y += vel;
                    bounds.moveY(vel);
                }

                distTraveled += Math.Abs(vel);

                if (distTraveled > maxdist || drawRect.Right < 0 || drawRect.Left >= map.getPixelWidth())
                {
                    alive = false;
                }
                else
                {
                    // Test walls based on direction (left, right)
                    if (horizontal)
                    {
                        int oldX = bounds.X, newX;
                        if (vel > 0)
                        {
                            newX = map.checkBoundsXRight(bounds, Direction.Right);
                        }
                        else
                        {
                            newX = map.checkBoundsXLeft(bounds, Direction.Left);
                        }

                        if (newX != oldX || bounds.Y != drawRect.Y)
                        {
                            alive = false;
                            return;
                        }
                    }
                    else
                    {
                        int oldY = bounds.Y, newY;
                        if (vel > 0)
                        {
                            newY = map.checkBoundsYDown(bounds, Direction.Right);
                        }
                        else
                        {
                            newY = map.checkBoundsYUp(bounds, Direction.Right);
                        }

                        if (newY != oldY)
                        {
                            alive = false;
                            return;
                        }
                    }

                    // Test collision with entites
                    foreach (Entity e in map.entityIterator())
                    {
                        if (!e.Alive)
                        {
                            continue;
                        }

                        EntityHit eHit;
                        if (horizontal)
                        {
                            eHit = e.EBounds.collide(new Point(bounds.Right, bounds.Center.Y));
                            // If not hit in front, check back
                            if (eHit.Part == EntityPart.None)
                            {
                                eHit = e.EBounds.collide(new Point(bounds.Left, bounds.Center.Y));
                            }
                        }
                        else
                        {
                            int y;
                            if (msSpeed > 0)
                            {
                                y = bounds.Bottom;
                            }
                            else
                            {
                                y = bounds.Top;
                            }

                            eHit = e.EBounds.collide(new Point(bounds.Left, y));
                            // If not hit on left, check right
                            if (eHit.Part == EntityPart.None)
                            {
                                eHit = e.EBounds.collide(new Point(bounds.Right, y));
                            }
                        }

                        if (eHit.Part != EntityPart.None)
                        {
                            alive = false;
                            if (eHit.Part != EntityPart.Miss)
                            {
                                float dmgReducer = 0;
                                if (eHit.PercFromCenter >= 0.75)
                                {
                                    dmgReducer = 0.25f;
                                }
                                else if (eHit.PercFromCenter >= 0)
                                {
                                    dmgReducer = 1 - eHit.PercFromCenter;
                                }

                                int realDmg = e.hitInThe(eHit.Part, dmg, dmgReducer);
                                realDmg += e.slide(eHit.KnockBack);

                                map.addHitText(e, realDmg);
                                if (!e.Alive)
                                {
                                    xp += e.XPValue;
                                }
                            }
                            return;
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public void update(TileMap map, TimeSpan elapsed)
        {
            if (alive)
            {
                lastElapsed = elapsed;

                if (horizontal)
                {
                    drawRect.X += getRealSpeed();
                    collRect.X += getRealSpeed();
                }
                else
                {
                    drawRect.Y += getRealSpeed();
                    collRect.Y += getRealSpeed();
                }

                distTraveled += Math.Abs(getRealSpeed());

                if (distTraveled > maxdist || drawRect.Right < 0 || drawRect.Left >= map.getPixelWidth())
                {
                    alive = false;
                }
                else
                {
                    // Test walls based on direction (left, right)
                    if (horizontal)
                    {
                        int x;
                        if (msSpeed > 0)
                        {
                            x = map.shrinkX(collRect.Right, false);
                        }
                        else
                        {
                            x = map.shrinkX(collRect.Left, false);
                        }

                        int maxY = map.shrinkY(collRect.Bottom, true);
                        for (int y = map.shrinkY(collRect.Top, false); y <= maxY; y++)
                        {
                            Rectangle wallRect = map.getRect(x, y);

                            if (map.checkCollision(collRect, wallRect))
                            {
                                alive = false;
                                return;
                            }
                        }
                    }
                    else
                    {
                        int y;
                        if (msSpeed > 0)
                        {
                            y = map.shrinkX(collRect.Bottom, false);
                        }
                        else
                        {
                            y = map.shrinkX(collRect.Top, false);
                        }

                        int maxX = map.shrinkX(collRect.Left, true);
                        for (int x = map.shrinkX(collRect.Right, false); x <= maxX; x++)
                        {
                            Rectangle wallRect = map.getRect(x, y);

                            if (map.checkCollision(collRect, wallRect))
                            {
                                alive = false;
                                return;
                            }
                        }
                    }

                    // Test collision with entites
                    foreach (Entity e in map.entityIterator())
                    {
                        if (!e.Alive)
                        {
                            continue;
                        }

                        EntityHit eHit;
                        if (horizontal)
                        {
                            eHit = e.EBounds.collide(new Point(collRect.Right, collRect.Center.Y));
                            // If not hit in front, check back
                            if (eHit.Part == EntityPart.None)
                            {
                                eHit = e.EBounds.collide(new Point(collRect.Left, collRect.Center.Y));
                            }
                        }
                        else
                        {
                            int y;
                            if (msSpeed > 0)
                            {
                                y = collRect.Bottom;
                            }
                            else
                            {
                                y = collRect.Top;
                            }

                            eHit = e.EBounds.collide(new Point(collRect.Left, y));
                            // If not hit on left, check right
                            if (eHit.Part == EntityPart.None)
                            {
                                eHit = e.EBounds.collide(new Point(collRect.Right, y));
                            }
                        }

                        if (eHit.Part != EntityPart.None)
                        {
                            alive = false;
                            if (eHit.Part != EntityPart.Miss)
                            {
                                float dmgReducer = ((eHit.PercFromCenter < 0.6) ? 1 - eHit.PercFromCenter : 0.4f);
                                int   realDmg    = e.hitInThe(eHit.Part, dmg, dmgReducer);
                                map.addHitText(e, realDmg);
                                if (!e.Alive)
                                {
                                    xp += e.XPValue;
                                }
                            }
                            return;
                        }
                    }
                }
            }
        }