Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Float na Integer, niszczenie obiektu
emSon
post
Post #1





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 28.03.2015

Ostrzeżenie: (0%)
-----


Siemka. Robię właśnie skrypcik naśladujący kowala w pewnej grze. W sumie wszystko chodzi , jednak natrafiłem na dwa myki.

  1. $upgrade = rand(0,100);
  2. if ($upgrade < $chance * 100) {
  3. $this->lvlupgr ++;
  4. $this->damage *= 1.2;
  5. $this->damage = round($this->damage, 0);
  6.  
  7. echo "Pomyslnie ulepszono przedmiot!";
  8.  
  9. } else { ...


Chodzi mi o to , aby damage nie zapisywało się jako float, ale była zaokrąglana do integer. Nie wychodzi.

  1. ... } else {
  2. unset(?);
  3. echo "Ulepszenie przedmiotu nie powiodlo sie i zostal usuniety z twojego ekwipunku";
  4. }


Tu chodzi o to, aby w razie niepowodzenia przedmiot( czyli obiekt) został usunięty z ekwipunku.
Dzięki z góry za pomoc/
Go to the top of the page
+Quote Post
Kshyhoo
post
Post #2





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




tak?
Go to the top of the page
+Quote Post
emSon
post
Post #3





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 28.03.2015

Ostrzeżenie: (0%)
-----


Ok, dzięki. A jak zniszczyć obiekt w funkcji?
Go to the top of the page
+Quote Post
Kshyhoo
post
Post #4





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




PHP/Konstruktory i destruktory
Go to the top of the page
+Quote Post
Pyton_000
post
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Żle się do tego zabierasz.

Obiekt Przedmiot powinien mieć zapisaną szansę na ulepszenie.
Obiekt Kowal dostaje jako parametr Przedmiot i wykonuje akcje "upgrade"
Ta albo zwraca true i robi magię na przedmiocie albo false.
Jeśli false to kowal niszczy przedmiot i nie zwraca go Graczowi.
Go to the top of the page
+Quote Post
emSon
post
Post #6





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 28.03.2015

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. class miecz {
  4. public $lvlupgr;
  5. public $mindamage;
  6. public $maxdamage;
  7.  
  8. const cost = 10;
  9.  
  10. const level0 = 1;
  11. const level1 = 2;
  12. const level2 = 3;
  13. const level3 = 4;
  14. const level4 = 5;
  15. const level5 = 6;
  16. const level6 = 7;
  17. const level7 = 8;
  18. const level8 = 9;
  19. const level9 = 10;
  20.  
  21. const damage0 = 10;
  22. const damage1 = 15;
  23. const damage2 = 20;
  24. const damage3 = 25;
  25. const damage4 = 30;
  26. const damage5 = 35;
  27. const damage6 = 40;
  28. const damage7 = 45;
  29. const damage8 = 50;
  30. const damage9 = 60;
  31.  
  32.  
  33.  
  34. function __construct($lvlupgr){
  35. $this->lvlupgr = $lvlupgr;
  36. if($this->lvlupgr == 0){$this->mindamage = round(miecz::damage0 * 0.8); $this->maxdamage = round(miecz::damage0 * 1.2);}
  37. if($this->lvlupgr == 1){$this->mindamage = round(miecz::damage1 * 0.8); $this->maxdamage = round(miecz::damage1 * 1.2);}
  38. if($this->lvlupgr == 2){$this->mindamage = round(miecz::damage2 * 0.8); $this->maxdamage = round(miecz::damage2 * 1.2);}
  39. if($this->lvlupgr == 3){$this->mindamage = round(miecz::damage3 * 0.8); $this->maxdamage = round(miecz::damage3 * 1.2);}
  40. if($this->lvlupgr == 4){$this->mindamage = round(miecz::damage4 * 0.8); $this->maxdamage = round(miecz::damage4 * 1.2);}
  41. if($this->lvlupgr == 5){$this->mindamage = round(miecz::damage5 * 0.8); $this->maxdamage = round(miecz::damage5 * 1.2);}
  42. if($this->lvlupgr == 6){$this->mindamage = round(miecz::damage6 * 0.8); $this->maxdamage = round(miecz::damage6 * 1.2);}
  43. if($this->lvlupgr == 7){$this->mindamage = round(miecz::damage7 * 0.8); $this->maxdamage = round(miecz::damage7 * 1.2);}
  44. if($this->lvlupgr == 8){$this->mindamage = round(miecz::damage8 * 0.8); $this->maxdamage = round(miecz::damage8 * 1.2);}
  45. if($this->lvlupgr == 9){$this->mindamage = round(miecz::damage9 * 0.8); $this->maxdamage = round(miecz::damage9 * 1.2);}
  46. }
  47.  
  48.  
  49. function pokaz(){
  50. echo "<p>Twoj miecz ma teraz $this->lvlupgr poziom ulepszenia i obrazenia na poziomie $this->mindamage - $this->maxdamage</p>";
  51. }
  52. }
  53.  
  54. class kowal {
  55. const chance1 = 0.90;
  56. const chance2 = 0.90;
  57. const chance3 = 0.90;
  58. const chance4 = 0.80;
  59. const chance5 = 0.70;
  60. const chance6 = 0.60;
  61. const chance7 = 0.50;
  62. const chance8 = 0.40;
  63. const chance9 = 0.30;
  64.  
  65. const cost1 = 0.20;
  66. const cost2 = 0.40;
  67. const cost3 = 0.60;
  68. const cost4 = 0.70;
  69. const cost5 = 1.00;
  70. const cost6 = 1.20;
  71. const cost7 = 1.40;
  72. const cost8 = 1.60;
  73. const cost9 = 1.80;
  74.  
  75. function ulepsz($miecz){
  76. if($miecz->lvlupgr < 9){
  77.  
  78. if($miecz->lvlupgr === 0){ $chance = kowal::chance1; $cost = kowal::cost1 * miecz::cost; $damage = miecz::damage1;}
  79. if($miecz->lvlupgr === 1){ $chance = kowal::chance2; $cost = kowal::cost2 * miecz::cost; $damage = miecz::damage2;}
  80. if($miecz->lvlupgr === 2){ $chance = kowal::chance3; $cost = kowal::cost3 * miecz::cost; $damage = miecz::damage3;}
  81. if($miecz->lvlupgr === 3){ $chance = kowal::chance4; $cost = kowal::cost4 * miecz::cost; $damage = miecz::damage4;}
  82. if($miecz->lvlupgr === 4){ $chance = kowal::chance5; $cost = kowal::cost5 * miecz::cost; $damage = miecz::damage5;}
  83. if($miecz->lvlupgr === 5){ $chance = kowal::chance6; $cost = kowal::cost6 * miecz::cost; $damage = miecz::damage6;}
  84. if($miecz->lvlupgr === 6){ $chance = kowal::chance7; $cost = kowal::cost7 * miecz::cost; $damage = miecz::damage7;}
  85. if($miecz->lvlupgr === 7){ $chance = kowal::chance8; $cost = kowal::cost8 * miecz::cost; $damage = miecz::damage8;}
  86. if($miecz->lvlupgr === 8){ $chance = kowal::chance9; $cost = kowal::cost9 * miecz::cost; $damage = miecz::damage9;}
  87.  
  88. $upgrade = rand(0,100);
  89.  
  90. if($upgrade < $chance * 100){
  91.  
  92. $miecz->lvlupgr++;
  93. echo "Pomyslnie ulepszono przedmiot, kosztowalo cie to ".$cost."YANG.";
  94.  
  95. if($miecz->lvlupgr == 1){$miecz->mindamage = round(miecz::damage1 * 0.8); $miecz->maxdamage = round(miecz::damage1 * 1.2);}
  96. if($miecz->lvlupgr == 2){$miecz->mindamage = round(miecz::damage2 * 0.8); $miecz->maxdamage = round(miecz::damage2 * 1.2);}
  97. if($miecz->lvlupgr == 3){$miecz->mindamage = round(miecz::damage3 * 0.8); $miecz->maxdamage = round(miecz::damage3 * 1.2);}
  98. if($miecz->lvlupgr == 4){$miecz->mindamage = round(miecz::damage4 * 0.8); $miecz->maxdamage = round(miecz::damage4 * 1.2);}
  99. if($miecz->lvlupgr == 5){$miecz->mindamage = round(miecz::damage5 * 0.8); $miecz->maxdamage = round(miecz::damage5 * 1.2);}
  100. if($miecz->lvlupgr == 6){$miecz->mindamage = round(miecz::damage6 * 0.8); $miecz->maxdamage = round(miecz::damage6 * 1.2);}
  101. if($miecz->lvlupgr == 7){$miecz->mindamage = round(miecz::damage7 * 0.8); $miecz->maxdamage = round(miecz::damage7 * 1.2);}
  102. if($miecz->lvlupgr == 8){$miecz->mindamage = round(miecz::damage8 * 0.8); $miecz->maxdamage = round(miecz::damage8 * 1.2);}
  103. if($miecz->lvlupgr == 9){$miecz->mindamage = round(miecz::damage9 * 0.8); $miecz->maxdamage = round(miecz::damage9 * 1.2);}
  104.  
  105. } else {
  106. unset($miecz);
  107. echo "<p>Ulepszenie nie powiodlo sie.</p>";
  108. }
  109. }
  110. }
  111. }
  112.  
  113. $kowal = new kowal;
  114. $mieczyk1 = new miecz(0);
  115.  
  116. $mieczyk1->pokaz();
  117. $kowal->ulepsz($mieczyk1);
  118. ?>

Przejrzy ktos kodzik i powie jak mozna by to bylo zrobic inaczej (IMG:style_emoticons/default/biggrin.gif) ? Myslalem , zeby if'y zamknac w for, albo while.
Dalej jednak nie moge znalezc sposob na destruktor.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


No toć Ci napisałem jaki powinien być flow.

Poza tym masz tu poglądowo "skróconą" klasę miecza
  1. class miecz {
  2.  
  3. const cost = 10;
  4.  
  5. protected $lvlupgr;
  6. protected $baseDamageUpgrade = 10;
  7. protected $baseDamegeUpgradeMultipler = 5;
  8. protected $minDamage = 1;
  9. protected $maxDamage = 5;
  10.  
  11. function __construct($lvlupgr){
  12. $this->lvlupgr = $lvlupgr;
  13.  
  14. $damage = $this->baseDamage + ($this->lvlupgr * $this->$baseDamegeUpgradeMultipler);
  15.  
  16. $this->minDamage = round($damage * 0.8);
  17. $this->maxDamage = round($damage * 1.2);
  18.  
  19. }
  20.  
  21. function pokaz(){
  22. echo "<p>Twoj miecz ma teraz $this->lvlupgr poziom ulepszenia i obrazenia na poziomie $this->mindamage - $this->maxdamage</p>";
  23. }
  24. }
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.10.2025 - 02:37