1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved String to 2DModel convert

Discussion in 'Development' started by GamakCZ, May 25, 2020.

  1. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Hello there, I'm trying to convert String to 2DModel. It should seems like that

    upload_2020-5-25_17-20-40.png
    but it should be moved behind the player.

    I used this model:
    Code:
       xxxx           xxxx
     xxx  xxx       xxx  xxx
    xx      xx     xx      xx
     xx      xx   xx      xx
      xx      xx xx      xx
       xx    x     x    xx
        xx    x   x    xx
       xx   xx     xx   xx
        xx xx       xx xx
         xxx         xxx
    
    And this code:
    PHP:
       /**
         * @param string $string
         * @param float $breakSize
         * @return Model2D
         */
        
    public static function convertStringTo2DModel(string $stringfloat $breakSize 0.1): Model2D {
            
    $lines explode("\n"$string);

            
    /** @var int $minX */
            
    $minX null;
            
    /** @var int $maxX */
            
    $maxX null;

            
    $maxY 0;
            foreach (
    $lines as $line) {
                
    $length strlen($line);
                if(
    $minX === null) {
                    
    $minX $length;
                }
                if(
    $maxX === null) {
                    
    $maxX $length;
                }

                
    $minX min($minX$length);
                
    $maxX max($maxX$length);

                
    $maxY++;
            }

            
    /** @var float $xx */
            
    $xx = ($maxX 2);
            
    /** @var float $yy */
            
    $yy = ($maxY 2) / 2;

            
    /** @var float $centerX */
            
    $centerX = ((int)($maxX-$minX) / 2) + $xx;
            
    /** @var Model2D $model */
            
    $model = new Model2D();

            
    $y 0;
            while (!empty(
    $lines)) {
                
    $last array_pop($lines);
                while (
    strlen($last) < $maxX) {
                    
    $last $last " ";
                }

                
    $spaces 0;
                for(
    $i 0$i strlen($last); $i++) {
                    if(
    $i === $centerX) {
                        
    $model->addPoint(new Vector2(0$y 7));
                        continue;
                    }

                    if(
    $last[$i] === " ") {
                        
    $spaces++;
                    }

                    if(
    $last[$i] === "x")
                        
    $model->addPoint(new Vector2(($i $centerX $i $centerX $i) / 7$y 7));
                }

                
    $y++;
            }

            
    var_dump($model);
            return 
    $model;
        }
    What did I do wrong?

     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Fixed, but idk how ._.

    PHP:
      /**
         * @param string $string
         * @param float $breakSize
         *
         * @return Model2D
         */
        
    public static function convertStringTo2DModel(string $stringfloat $breakSize 0.1): Model2D {
            
    $lines explode("\n"$string);
            
    var_dump($string);

            
    /** @var int $minX */
            
    $minX null;
            
    /** @var int $maxX */
            
    $maxX null;

            foreach (
    $lines as $line) {
                
    $length strlen($line);
                if(
    $minX === null) {
                    
    $minX $length;
                }
                if(
    $maxX === null) {
                    
    $maxX $length;
                }

                
    $minX min($minX$length);
                
    $maxX max($maxX$length);
            }

            
    /** @var float $centerX */
            
    $centerX = ((int)($maxX-$minX) / 2) + ($maxX 2);
            
    /** @var Model2D $model */
            
    $model = new Model2D();
            
            
    $y 0;
            while (!empty(
    $lines)) {
                
    /** @var string $last */
                
    $last array_pop($lines);
                while (
    strlen($last) < $maxX) {
                    
    $last $last " ";
                }

                
    $spaces 0;
                for(
    $i 0$i strlen($last); $i++) {
                    if(
    $last[$i] === " ") {
                        
    $spaces++;
                    }
                    if(
    $last[$i] === "x") {
                        
    $model->addPoint(new Vector2(($i - ($centerX)) / 7$y 7));
                    }
                }

                
    $y++;
            }

            return 
    $model;
        }
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.