Пример #1
0
        public void update(Platforms platforms, Obstacles obstacles, Obstacles spikes)
        {
            if(goBottom) {
                if(speed.Y < 40)
                    speed.Y += 2*Game1.gameSpeed;
                goTop = false;
                if(speed.Y > 5)
                    state = Character.STATE_SKY_DOWN;
            }
            if(goTop) {
                if(speed.Y > -40)
                    speed.Y -= 2*Game1.gameSpeed;
                goBottom = false;
                if(speed.Y < -5)
                    state = Character.STATE_SKY_UP;
            }
            if(curPlatforms != -1) {
                Platform platform = platforms.getPlatform(curPlatforms);
                if(platform.getType() == Platform.TYPE_GROUND) {
                    if(goTop)
                        speed.Y = 0;
                    goBottom = true;
                    goTop = false;
                    if(bound.Intersects(platform.getBound())){
                        if(getCollisionType(platform.getBound()) == Character.COLLISION_Y) {
                            speed.Y = 0;
                            goBottom = false;
                            position.Y = platform.getBound().Top - Character.HEIGHT+1;
                            state = Character.STATE_BOTTOM;
                            if(position.X < Character.DEFAULT_POS_X) {
                                speed.X = 3;
                            }
                        }
                        else if(getCollisionType(platform.getBound()) == Character.COLLISION_X){
                            position.X = platform.getBound().Left - Character.WIDTH + 1;
                        }
                    }
                }
                else {
                    if(goBottom)
                        speed.Y = 0;
                    goTop = true;
                    goBottom = false;
                    if(bound.Intersects(platform.getBound())) {
                        speed.Y = 0;
                        goTop = false;
                        position.Y = platform.getBound().Bottom -1;
                        state = Character.STATE_TOP;
                        if(position.X < Character.DEFAULT_POS_X) {
                            speed.X = 3;
                        }
                    }
                }
            }
            else {
                goBottom = true;
                if(goTop) {
                    goTop = false;
                    speed.Y = 0;
                }

            }
            if(position.X > Character.DEFAULT_POS_X) {
                position.X = Character.DEFAULT_POS_X;
                speed.X = 0;
            }
            int n = obstacles.getSize();
            int i = 0;
            Rectangle rect;
            while(i < n) {
                rect = obstacles.getObstacle(i).getBound();
                if(bound.Intersects(rect)){
                    if(getCollisionType(rect) == Character.COLLISION_Y) {
                        if(drawBound.Center.Y < rect.Center.Y  && (state == Character.STATE_SKY_DOWN || state == Character.STATE_BOTTOM)) {
                            speed.Y = 0;
                            goBottom = false;
                            position.Y = rect.Top - Character.HEIGHT + 1;
                        }
                        else if(drawBound.Center.Y > rect.Center.Y && (state == Character.STATE_SKY_UP || state == Character.STATE_TOP)) {
                            speed.Y = 0;
                            position.Y = rect.Bottom - 1;
                        }
                        if(position.X < Character.DEFAULT_POS_X) {
                            speed.X = 3;
                        }
                        if(position.X > Character.DEFAULT_POS_X) {
                            position.X = Character.DEFAULT_POS_X;
                            speed.X = 0;
                        }
                    }
                    else if(getCollisionType(rect) == Character.COLLISION_X && drawBound.Center.X < rect.Center.X){
                        position.X = rect.Left - Character.WIDTH + 1;
                    }
                }
                i++;
            }

            for(i = 0; i < n;i++ ) {
                rect = obstacles.getObstacle(i).getDeathBound();
                if(bound.Intersects(rect)) {
                    if (!songstart)
                    {
                        MediaPlayer.Play(Assets.ScreamBGM);
                        songstart = true;
                    }
                    songstart = false;
                    MediaPlayer.Stop();
                    health--;
                    position.X = Character.DEFAULT_POS_X;
                    position.Y = Platform.DEFAULT_SKY_Y + Platform.SIZE_TYPE_1.Y;
                    songstart = false;
                    MediaPlayer.Stop();
                }
                i++;
            }
            if(position.Y > Game1.screenSize.Y + 70 || position.X < -100) {
                position.Y = Game1.screenSize.Y - 200;
                if(health > 0)
                    position.X = Character.DEFAULT_POS_X;
                platforms.addPlatform(position.X + 30, Platform.DEFAULT_GROUND_Y, Platform.SIZE_TYPE_1);
                speed.Y = 0;
                health--;
            }
            distance += (-1 * Background.simultanSpeed.X);
            position += (speed * Game1.gameSpeed);
            bound.Offset((int)(position.X - bound.Left), (int)(position.Y - bound.Top));
            drawBound.Offset((int)(position.X - drawBound.Left), (int)(position.Y - drawBound.Top));
            colliBound.Offset((int)(10 + position.X - colliBound.Left), (int)(10 + position.Y - colliBound.Top));
        }