|
|
|
PHP Battle System | Kampf Script v.2 Beta
Zurück zur Übersicht Diese Klasse erstellt rundenbasierte Kämpfe, die mit unbegrenzten Möglichkeiten verändert werden können.
Tutorial befindet sich unter dem code.
class.battle.php:
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
|
<?php
/************************************************************************
* Battle script *
* version 2.0 beta *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
* *
* Copyright <makesite.de> 2011 *
************************************************************************/
class Battle {
/*
* @class Battle
*
* @function __construct
* @function attack
* @function defence
* @function add_group
* @function add_warrior
* @function start
* @function duel
* @function deathmatch
* @function team_deathmatch
* @function debug
*/
const duel = 0x0000; // fight types
const deathmatch = 0x0001;
const team_deathmatch = 0x0002;
const warrior_name = 0x0100; // warrior datas
const warrior_hp = 0x0200;
const warrior_off = 0x0300;
const warrior_off_m = 0x0500;
const warrior_def = 0x0600;
const warrior_def_m = 0x0800;
const warrior_group = 0x0900;
const red = "#FF0000"; // colors for teams
const blue = "#0000FF";
const green = "#00FF00";
const yellow = "#FFFF00";
private $warrior_set = array(); // warrior set
private $warrior_num = 0; // warrior count
private $warrior_main = array( // warrior default set
self::warrior_name => "Warrior",
self::warrior_hp => 100,
self::warrior_off => 10,
self::warrior_off_m => 1,
self::warrior_def => 10,
self::warrior_def_m => 1,
self::warrior_group => null
);
private $group_current = null;
private $group_set = array();
private $group_num = 0;
private $save_log = array(); // saved log
private $type = self::duel; // type set
private $random_skills = true;
/*
* @function __construct
* @var $type
* @var $random_skills
*
* Initialisierung des gesamten Kampfes und Festlegung des Kampftypes. Als option kann festgelegt werden,
* ob die Angriffs- und Verteidungswerte konstant oder mit einem kleinen Zufallseinluss genereriert werden
* sollen.
*/
function __construct($type=self::duel, $random_skills=true) {
$this->type=$type; // set type of battle
$this->random_skills=$random_skills; // set random manupilation
}
/*
* @function attack
* @var $stack
*
* Diese Funktion berechnet den endgültigen Angriffswert des Angreifers. Alle Waffen, Fähigkeiten
* sowie Rüstungen werden zusammengerechnet und als Zahl zurückgegeben.
*/
function attack(array $stack) {
if($this->random_skills) // check if attack is random
$attack = ($stack[self::warrior_off] * $stack[self::warrior_off_m]) * (rand(1,5)/10);
else
$attack = $stack[self::warrior_off] * $stack[self::warrior_off_m];
return round($attack);
}
/*
* @function defence
* @var $stack
*
* Diese Funktion berechnet den endgültigen Verteidigungswert des Verteidigers. Alle Waffen, Fähigkeiten
* sowie Rüstungen werden zusammengerechnet und als Zahl zurückgegeben.
*/
function defence(array $stack) {
if($this->random_skills) // check if defence is random
$attack = ($stack[self::warrior_def] * $stack[self::warrior_def_m]) * (rand(1,5)/10);
else
$attack = $stack[self::warrior_def] * $stack[self::warrior_def_m];
return round($attack);
}
/*
* @function add_group
* @var $name
* @var $color
*
* Gruppe wird hinzugefügt und es werden nur Krieger nur zu dieser Gruppe zugeordnet, bis eine neue
* erstellt wird.
*/
function add_group($name,$color) {
$this->group_current = count($this->group_set); // set current group
$this->group_num++;
array_push($this->group_set, array("name"=>$name,"color"=>$color,"warriors"=>array())); // add group
}
/*
* @function add_warrior
* @var stack
*
* Krieger wird hinzugefügt. Standardwerte und aktualisierte Werte werden zusammengefügt und
* übermittelt.
*/
function add_warrior(array $stack) {
$name = $this -> warrior_main[self::warrior_name]; // set defaults
$hp = $this -> warrior_main[self::warrior_hp];
$off = $this -> warrior_main[self::warrior_off];
$off_m = $this -> warrior_main[self::warrior_off_m];
$def = $this -> warrior_main[self::warrior_def];
$def_m = $this -> warrior_main[self::warrior_def_m];
$group = $this -> warrior_main[self::warrior_group];
if(array_key_exists(self::warrior_name, $stack)) // manual sets
$name = $stack[self::warrior_name];
if(array_key_exists(self::warrior_hp, $stack))
$hp = $stack[self::warrior_hp];
if(array_key_exists(self::warrior_off, $stack))
$off = $stack[self::warrior_off];
if(array_key_exists(self::warrior_off_m, $stack))
$off_m = $stack[self::warrior_off_m];
if(array_key_exists(self::warrior_def, $stack))
$def = $stack[self::warrior_def];
if(array_key_exists(self::warrior_def_m, $stack))
$def_m = $stack[self::warrior_def_m];
if(array_key_exists(self::warrior_def_m, $stack))
$group = $stack[self::warrior_group];
$r_stack = array( // result stack
self::warrior_name => $name,
self::warrior_hp => $hp,
self::warrior_off => $off,
self::warrior_off_m => $off_m,
self::warrior_def => $def,
self::warrior_def_m => $def_m
);
$this -> warrior_num ++; // count warrior
$this -> warrior_set[count($this->warrior_set)] = $r_stack; // save warrior
if($this->type==self::team_deathmatch) { // sort in a group if team deathmatch
array_push($this->group_set[$this->group_current]['warriors'], count($this->warrior_set)-1);
}
}
/*
* @function start
* @var $random
* @var $random_offender
* @var $max_alive
*
* Kampf wird gestartet. $random bietet entweder abwechselnde Angreifer oder zufällige Angreifer.
* Mit $random_offender wird festgelegt ob der erste Krieger oder ein zufälliger Krieger als Erster
* angreift.
*/
function start($random=false, $random_offender=false, $max_alive=1) {
$this->save_log['type'] = $this->type; // save type of match
$this->save_log['group'] = $this->group_set; // save set of groups
$this->save_log['warriors'] = $this->warrior_set; // save set of groups
switch($this->type) { // check which type
case self::duel: // duel
if($this->warrior_num == 2)
$this->duel($random, $random_offender);
break;
case self::deathmatch: // deathmatch
$this->deathmatch($random,$random_offender,$max_alive);
break;
case self::team_deathmatch: // team_deathmatch
$this->team_deathmatch($random, $random_offender, $max_alive);
break;
}
}
/*
* @function duel
* @var $random
* @var $random_offender
*
* Kampftyp Duel
*/
function duel($random, $random_offender) {
$turn = (!$random_offender)?0:rand(0,1); // commit offender
$log = array(); // initializing log
$round = 0; // round
while($this->warrior_num == 2) { // fight starts
$round++; // count next round
$offender = $this->warrior_set[$turn]; // load offender datas
$defender = ($turn==1)?$this->warrior_set[0]:$this->warrior_set[1]; // load defender datas
$attack = $this->attack($offender); // generate attack and defence
$defence = $this->defence($defender);
$damage = (($attack-$defence)<=0)?0:$attack-$defence; // damage product
$this->warrior_set[($turn==1)?0:1][self::warrior_hp] = $defender[self::warrior_hp]-$damage; // update hp
//////////////////////////////////////////////////////////////////////////////////////////////////////
// SAVE LOG OF FIGHT // save log
//////////////////////////////////////////////////////////////////////////////////////////////////////
$log_count = count($log);
$log[$log_count]['round'] = $round; // round
$log[$log_count]['offender'] = $offender; // offender
$log[$log_count]['defender'] = $defender; // defender
$log[$log_count]['attack'] = $attack; // attack
$log[$log_count]['defence'] = $defence; // defence
$log[$log_count]['damage'] = $damage; // damage
$log[$log_count]['def_hp'] = $this->warrior_set[($turn==1)?0:1][self::warrior_hp]; // defender hp
if($this->warrior_set[($turn==1)?0:1][self::warrior_hp]<=0) // check whether the defender is dead
{
$this->warrior_num = 1; // warrior count to 1
$this->save_log['log'] =&$log; // save log
$this->save_log['winner']=$offender[self::warrior_name];
}
if($random) // generate next offender
$turn = rand(0,1);
else
$turn = ($turn==0)?1:0;
}
}
/*
* @function deathmatch
* @var $random
* @var $random_offender
* @var $max_alive
*
* Kampftyp deathmatch
*/
function deathmatch($random, $random_offender, $max_alive) {
$turn = (!$random_offender)?0:rand(0,$this->warrior_num-1); // commit offender
$log = array(); // initializing log
$round = 0; // round
while($this->warrior_num > $max_alive) { // fight starts
$round++; // count next round
$offender = $this->warrior_set[$turn]; // load offender datas
$rand_def = rand(0,$this->warrior_num-1); // select defender
if($rand_def==$turn) {
if($rand_def==$this->warrior_num-1) {
$rand_def = $rand_def -1;
$defender = $this -> warrior_set[$rand_def];
} else {
$rand_def = $rand_def +1;
$defender = $this -> warrior_set[$rand_def];
}
} else {
$rand_def = $rand_def +0;
$defender = $this -> warrior_set[$rand_def];
}
$attack = $this->attack($offender); // generate attack and defence
$defence = $this->defence($defender);
$damage = (($attack-$defence)<=0)?0:$attack-$defence; // damage product
$this->warrior_set[$rand_def][self::warrior_hp] = $defender[self::warrior_hp]-$damage; // update hp
//////////////////////////////////////////////////////////////////////////////////////////////////////
// SAVE LOG OF FIGHT // save log
//////////////////////////////////////////////////////////////////////////////////////////////////////
$log_count = count($log);
$log[$log_count]['round'] = $round; // round
$log[$log_count]['offender'] = $offender; // offender
$log[$log_count]['defender'] = $defender; // defender
$log[$log_count]['attack'] = $attack; // attack
$log[$log_count]['defence'] = $defence; // defence
$log[$log_count]['damage'] = $damage; // damage
$log[$log_count]['def_hp'] = $defender[self::warrior_hp]-$damage; // defender hp
if($this->warrior_set[$rand_def][self::warrior_hp]<=0) // check whether the defender is dead
{
$this->warrior_num--; // warrior count to 1
$this->save_log['log'] =&$log; // save log
unset($this->warrior_set[$rand_def]);
$this->warrior_set=array_merge($this->warrior_set);
}
if($random) { // generate next offender
$turn = rand(0,$this->warrior_num-1);
} else {
$turn++;
$turn = ($turn>=count($this->warrior_set))?0:$turn;
}
if($this->warrior_num == $max_alive) {
$this->save_log['winner']=$this->warrior_set;
}
}
}
/*
* @function team_deathmatch
* @var $random
* @var $random_offender
* @var $max_alive
*
* Kampftyp team deathmatch
*/
function team_deathmatch($random, $random_offender, $max_alive) {
$turn = (!$random_offender)?0:rand(0,$this->group_num-1); // commit offender
$log = array(); // initializing log
$round = 0; // round
while($this->group_num > $max_alive) { // fight starts
$round++; // count next round
$offender = array_rand($this->group_set[$turn]['warriors']); // load offender datas
$offender = $this -> warrior_set[$this->group_set[$turn]['warriors'][$offender]];
$rand_def = rand(0,$this->group_num-1); // select defender
if($rand_def==$turn) { // if defender is offender
if($rand_def==$this->group_num-1) {
$rand_def = $rand_def -1;
$rand_def2_group = array_rand($this->group_set[$rand_def]['warriors']);
$rand_def2 = $this->group_set[$rand_def]['warriors'][$rand_def2_group];
$defender = $this -> warrior_set[$rand_def2];
} else {
$rand_def = $rand_def +1;
$rand_def2_group = array_rand($this->group_set[$rand_def]['warriors']);
$rand_def2 = $this->group_set[$rand_def]['warriors'][$rand_def2_group]; // load offender datas
$defender = $this -> warrior_set[$rand_def2];
}
} else {
$rand_def = $rand_def +0;
$rand_def2_group = array_rand($this->group_set[$rand_def]['warriors']);
$rand_def2 = $this->group_set[$rand_def]['warriors'][$rand_def2_group];
$defender = $this -> warrior_set[$rand_def2];
}
$attack = $this->attack($offender); // generate attack and defence
$defence = $this->defence($defender);
$damage = (($attack-$defence)<=0)?0:$attack-$defence; // damage product
$this->warrior_set[$rand_def2][self::warrior_hp] = $defender[self::warrior_hp]-$damage; // update hp
//////////////////////////////////////////////////////////////////////////////////////////////////////
// SAVE LOG OF FIGHT // save log
//////////////////////////////////////////////////////////////////////////////////////////////////////
$log_count = count($log);
$log[$log_count]['round'] = $round; // round
$log[$log_count]['offender'] = $offender; // offender
$log[$log_count]['offender_group'] = $turn; // offender group
$log[$log_count]['defender'] = $defender; // defender
$log[$log_count]['defender_group'] = $rand_def; // defender group
$log[$log_count]['attack'] = $attack; // attack
$log[$log_count]['defence'] = $defence; // defence
$log[$log_count]['damage'] = $damage; // damage
$log[$log_count]['def_hp'] = $defender[self::warrior_hp]-$damage; // defender hp
if($this->warrior_set[$rand_def2][self::warrior_hp]<=0) // check whether the defender is dead
{
$this->warrior_num--; // warrior count to 1
unset($this->group_set[$rand_def]['warriors'][$rand_def2_group]);
$this->group_set[$rand_def]['warriors']=array_merge($this->group_set[$rand_def]['warriors']);
if(count($this->group_set[$rand_def]['warriors'])==0) {
$this->group_num--;
unset($this->group_set[$rand_def]);
$this->group_set=array_merge($this->group_set);
}
}
if($random) { // generate next offender
$turn = rand(0,$this->group_num-1);
} else {
$turn++;
$turn = ($turn>=count($this->group_set))?0:$turn;
}
$this->save_log['log'] =&$log; // save log
if($this->group_num <= $max_alive) {
$this->save_log['winner'] = $this -> group_set;
}
}
}
/*
* @function debug
* @var $log
*
* Gibt den eingegangenen Log in Text aus.
*/
function debug($log) {
switch($log['type']) { // check which type of log
case self::team_deathmatch: // team deathmatch
echo "<h1>Team Deathmatch</h1><br>";
echo "<h2>Gewinner: ";
for($i=0;$i<count($log['winner']);$i++) { // list all winners
$group = $log['winner'][$i];
$color = $group['color'];
$name = $group['name'];
echo "<span style=\"color:$color\">$name</span> ";
}
echo "</h2><br>";
echo "<table style=\"width:100%\" border=\"1\"><tr>";
for($i=0;$i<count($log['group']);$i++) { // list teams
$group = $log['group'][$i];
$color = $group['color'];
echo "<td><span style=\"color:$color\">";
echo $group['name'];
echo "</span><hr>";
for($i2=0;$i2<count($group['warriors']);$i2++) {
echo $log['warriors'][$group['warriors'][$i2]][self::warrior_name]."<br>";
}
echo "</td>";
}
echo "</tr></table><br><br>";
for($i=0;$i<count($log['log']);$i++) { // log output
$off_group = $log['log'][$i]['offender_group'];
$def_group = $log['log'][$i]['defender_group'];
echo "Runde: ";
echo $log['log'][$i]['round'];
echo "<br>";
echo "<span style=\"color:".$log['group'][$off_group]['color']."\">";
echo $log['log'][$i]['offender'][self::warrior_name];
echo "</span>";
echo " greift ";
echo "<span style=\"color:".$log['group'][$def_group]['color']."\">";
echo $log['log'][$i]['defender'][self::warrior_name];
echo "</span>";
echo " an und richtet ";
echo $log['log'][$i]['damage'];
echo " Schaden an. Lebenspunkte von ";
echo $log['log'][$i]['defender'][self::warrior_name];
echo ": ";
echo $log['log'][$i]['def_hp'];
echo "<br>";
if($log['log'][$i]['def_hp']<=0) {
echo "<br><b>".$log['log'][$i]['defender'][self::warrior_name];
echo " ist tot.</b><br>";
}
echo "<br>";
}
break;
case self::deathmatch: // deathmatch
echo "<h1>Deathmatch</h1><br>";
echo "<h2>Gewinner: ";
for($i=0;$i<count($log['winner']);$i++) { // list of winners
$group = $log['winner'][$i];
$name = $group[self::warrior_name];
if($i==count($log['winner'])-1)
echo "$name";
else
echo "$name, ";
}
echo "</h2><br>";
for($i=0;$i<count($log['log']);$i++) { // log output
$off_group = $log['log'][$i]['offender_group'];
$def_group = $log['log'][$i]['defender_group'];
echo "Runde: ";
echo $log['log'][$i]['round'];
echo "<br>";
echo "<span style=\"color:".$log['group'][$off_group]['color']."\">";
echo $log['log'][$i]['offender'][self::warrior_name];
echo "</span>";
echo " greift ";
echo "<span style=\"color:".$log['group'][$def_group]['color']."\">";
echo $log['log'][$i]['defender'][self::warrior_name];
echo "</span>";
echo " an und richtet ";
echo $log['log'][$i]['damage'];
echo " Schaden an. Lebenspunkte von ";
echo $log['log'][$i]['defender'][self::warrior_name];
echo ": ";
echo $log['log'][$i]['def_hp'];
echo "<br>";
if($log['log'][$i]['def_hp']<=0) {
echo "<b>".$log['log'][$i]['defender'][self::warrior_name];
echo " ist tot.</b><br>";
}
echo "<br>";
}
break;
case self::duel: // duel
echo "<h1>Duel</h1>";
echo "<h2>Gewinner: ".$log['winner'].'</h2><br>'; // winner
for($i=0;$i<count($log['log']);$i++) { // log output
$off_group = $log['log'][$i]['offender_group'];
$def_group = $log['log'][$i]['defender_group'];
echo "Runde: ";
echo $log['log'][$i]['round'];
echo "<br>";
echo "<span style=\"color:".$log['group'][$off_group]['color']."\">";
echo $log['log'][$i]['offender'][self::warrior_name];
echo "</span>";
echo " greift ";
echo "<span style=\"color:".$log['group'][$def_group]['color']."\">";
echo $log['log'][$i]['defender'][self::warrior_name];
echo "</span>";
echo " an und richtet ";
echo $log['log'][$i]['damage'];
echo " Schaden an. Lebenspunkte von ";
echo $log['log'][$i]['defender'][self::warrior_name];
echo ": ";
echo $log['log'][$i]['def_hp'];
echo "<br>";
if($log['log'][$i]['def_hp']<=0) {
echo "<b>".$log['log'][$i]['defender'][self::warrior_name];
echo " ist tot.</b><br>";
}
echo "<br>";
}
break;
}
}
/*
* @function getLog
*
* Gibt den gespeicherten Log der geöffneten Instanz zurück.
*/
function getLog() {
return $this->save_log;
}
}
|
|
Tutorial
Schritt 1: Klasse instanzieren
Üblicherweise wird die Klasse zunächst instanziert. Als Parameter werden Kampftyp und ein Boolean für den Zufallseinluss im Angriffs- und Verteidigungswert an den Konstruktor __construct() übergeben.
In dieser Version sind zwei neue Kampftypen dazugekommen. Ich liste also alle drei Möglichkeiten auf:
Duel (Battle::duel)
Ein Kampf zwischen zwei Gegnern. Es gewinnt derjenige, der am Leben bleibt.
Deathmatch (Battle::deathmatch)
Kampf zwischen mehreren Spielern. Kampf endet, sobald die Anzahl der maximal Überlebenden erreicht ist.
Team Deathmatch (Battle::team_deathmatch)
Kampfzwischen mehreren Spielern. Die darin enthaltende Diplomatie Funktion ermöglicht Kämpfe mit mehreren Teams gegeneinander. Auch hier hört der Kampf erst auf, sobald die Anzahl der maximal Überlebenden erreich ist.
Ein sinnvolles Beispiel für die Instanzierung wäre sowas:
| PHP |
1:
2:
|
<?php
$battle = new Battle(Battle::team_deathmatch, true);
|
|
Deutung:
Der Kampftyp ist Team Deathmatch und die Angriffs und Verteidigungswerte sind nicht konstant, also mit einem kleinen Zufallseinfluss versehen.
| Zitat: |
TIPP:
Empfehlenswert ist es den Zufallseinfluss zu aktivieren, andernfalls könnte ein Kampf sich zu einer endlosschleife bilden, wenn die Gegner gleichstarke Fähigkeiten haben. |
Schritt zwei: Krieger bilden
Um neue Krieger zu erstellen, reicht schon ein Array aus. Die klassenbezogenen Konstanten helfen beim Programmieren, die richtige Fähigkeiten zu setzen.
Ein Krieger hat 7 optionale Eigenschaften:
Name (Battle::warrior_name)
Standardwert: "Warrior"
Der Name des Kriegers
Gruppe (Battle::warrior_group) (nur im Team Deathmatch Modus benötigt)
Standardwert: null
Das Team, in dem sich der Krieger befindet
Lebenspunkte (Battle::warrior_hp)
Standardwert: 100
Die Lebenspunkte des Kriegers.
Attacke (Battle::warrior_off)
Standardwert: 10
Der normale Angriffswert des Spielers.
Attacke Multiplikator (Battle::warrior_off_m)
Standardwert: 1
Der Multiplikator, mit dem der Angriffswert multipliziert wird. Diese Eigenschaft dient hauptsächlich der Balanzierung.
Verteidigung (Battle::warrior_def)
Standardwert: 10
Der normale Verteidigungswert des Spielers.
Verteidigung Multiplikator (Battle::warrior_def_m)
Standardwert: 1
Der Multiplikator, mit dem der Verteidigungswert multipliziert wird. Diese Eigenschaft dient hauptsächlich der Balanzierung.
Das vorherige Beispiel wird fortgesetzt:
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
|
<?php
$battle = new Battle(Battle::team_deathmatch, true);
$warrior1 = array(
Battle::warrior_name => "Mensch",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 50,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
$warrior2 = array(
Battle::warrior_name => "Kobold",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 40,
Battle::warrior_off => 12,
Battle::warrior_off_m => 1.3,
Battle::warrior_def => 6,
Battle::warrior_def_m => 1.5
);
$warrior3 = array(
Battle::warrior_name => "Elf",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 100,
Battle::warrior_off => 5,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 10,
Battle::warrior_def_m => 1.6
);
$warrior4 = array(
Battle::warrior_name => "Zwerg",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 30,
Battle::warrior_off => 30,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 7,
Battle::warrior_def_m => 1.2
);
$warrior5 = array(
Battle::warrior_name => "Vampir",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 50,
Battle::warrior_off => 15,
Battle::warrior_off_m => 1.8,
Battle::warrior_def => 3,
Battle::warrior_def_m => 1.1
);
$warrior6 = array(
Battle::warrior_name => "Drache",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 140,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
|
|
| Zitat: |
Hinweis:
Die Angriffs und Verteidigungswerte werden nicht automatisch ausbalanziert. Das bedeutet, dass bei einer schlechter Balanzierung es zu vielen Runden kommen kann.
|
| Zitat: |
Tipp:
Je höher die Angriffswerte und je kleiner die Lebenspunkte, desto kürzere Logs.
|
Schritt drei: Krieger hinzufügen und Diplomatie zuordnen
Nachdem die Instanzierung durchgeführt worden ist, und die Krieger erstellt wurden, folgt nun die Einstellung des Kampfes.
Grundsätzlich werden Krieger über die funktion $battle->add_warrior() hinzugefügt. Wenn aber der Modus Team Deathmatch verwendet wird, wie in diesem Fall, müssen die Krieger in Gruppen zugeordnet werden.
Über die Methode ->add_group() erstellt man ein neues Team und alle Krieger, die als nächstes hinzugefügt werden, kommen in dieses Team.
Daraus ergibt sich:
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
|
<?php
$battle = new Battle(Battle::team_deathmatch, true);
$warrior1 = array(
Battle::warrior_name => "Mensch",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 50,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
$warrior2 = array(
Battle::warrior_name => "Kobold",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 40,
Battle::warrior_off => 12,
Battle::warrior_off_m => 1.3,
Battle::warrior_def => 6,
Battle::warrior_def_m => 1.5
);
$warrior3 = array(
Battle::warrior_name => "Elf",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 100,
Battle::warrior_off => 5,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 10,
Battle::warrior_def_m => 1.6
);
$warrior4 = array(
Battle::warrior_name => "Zwerg",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 30,
Battle::warrior_off => 30,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 7,
Battle::warrior_def_m => 1.2
);
$warrior5 = array(
Battle::warrior_name => "Vampir",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 50,
Battle::warrior_off => 15,
Battle::warrior_off_m => 1.8,
Battle::warrior_def => 3,
Battle::warrior_def_m => 1.1
);
$warrior6 = array(
Battle::warrior_name => "Drache",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 140,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
$battle -> add_group("Team A", Battle::blue);
$battle -> add_warrior($warrior1);
$battle -> add_warrior($warrior2);
$battle -> add_group("Team B", Battle::red);
$battle -> add_warrior($warrior3);
$battle -> add_warrior($warrior4);
$battle -> add_group("Team C", Battle::green);
$battle -> add_warrior($warrior5);
$battle -> add_warrior($warrior6);
|
|
| Zitat: |
Hinweis:
Die Namen der Teams müssen bei den Kriegern und den Gruppen übereinstimmen. |
| Zitat: |
Tipp:
Die Farben der Teams können sowohl über die klassenbezogenen Konstanten (Battle::red, Battle::blue, Battle::green, Battle::yellow) als auch eigene Farbcodes (#FFFFFF, #000000, s.o.) festgelegt werden. |
Schritt vier: Kampf starten
Als nächstes wird der Kampf über die Methode ->start() durchgeführt.
Als Parameter kann man 3 Variablen übergeben:
Zufällige Angreifer ($random)
empfohlen: false
Über diese Variabel lässt es sich festlegen, ob die Krieger nacheinander konstant nach ihrer Reihenfolge sich gegenseitig angreifen oder nach jeder Runde zufällig ein Angreifer ausgewählt wird.
Zufälliger erster Angreifer ($random_offender)
empfohlen: false
Hier geht es um den ersten Angreifer in der ersten Runde. Wird der Wert auf false gesetzt, greift der erst hinzugefügte Krieger an. Wenn nicht, wird irgendein Krieger ausgewählt.
Maximal Überlebende ($random) (nur in (Team) Deathmatch)
empfohlen: 1
Die Zahl, die angibt, wann der Kampf aufhört, d.h. wie viele am Ende überleben bleiben sollen. Gewöhnlicherweise ist der Wert 1.
So sieht das Beispiel nun aus:
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
|
<?php
$battle = new Battle(Battle::team_deathmatch, true);
$warrior1 = array(
Battle::warrior_name => "Mensch",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 50,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
$warrior2 = array(
Battle::warrior_name => "Kobold",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 40,
Battle::warrior_off => 12,
Battle::warrior_off_m => 1.3,
Battle::warrior_def => 6,
Battle::warrior_def_m => 1.5
);
$warrior3 = array(
Battle::warrior_name => "Elf",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 100,
Battle::warrior_off => 5,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 10,
Battle::warrior_def_m => 1.6
);
$warrior4 = array(
Battle::warrior_name => "Zwerg",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 30,
Battle::warrior_off => 30,
Battle::warrior_off_m => 1.7,
Battle::warrior_def => 7,
Battle::warrior_def_m => 1.2
);
$warrior5 = array(
Battle::warrior_name => "Vampir",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 50,
Battle::warrior_off => 15,
Battle::warrior_off_m => 1.8,
Battle::warrior_def => 3,
Battle::warrior_def_m => 1.1
);
$warrior6 = array(
Battle::warrior_name => "Drache",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 140,
Battle::warrior_off => 10,
Battle::warrior_off_m => 1.2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 1.6
);
$battle -> add_group("Team A", Battle::blue);
$battle -> add_warrior($warrior1);
$battle -> add_warrior($warrior2);
$battle -> add_group("Team B", Battle::red);
$battle -> add_warrior($warrior3);
$battle -> add_warrior($warrior4);
$battle -> add_group("Team C", Battle::green);
$battle -> add_warrior($warrior5);
$battle -> add_warrior($warrior6);
$battle -> start(false, false, 1);
|
|
Schritt fünf: Kampflog ausgeben
Der letzte Schritt ist einfach, wenn man die vorprogrammierte Methode benutzt. Der Log wird als Array gespeichert. Das Array enthält alle Informationen der Runden, Krieger, Gruppen und mehr. Diese Informationen können über ->debug() ausgegeben werden. Das Log ist über ->getLog() erreichbar.
Das Beispiel zusammengefasst:
Vorschau: http://dev.desc-net.de/products/bcreator/use.battle.php
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
|
<?php
$battle = new Battle(Battle::team_deathmatch, true);
$warrior1 = array(
Battle::warrior_name => "Mensch",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 50,
Battle::warrior_off => 10,
Battle::warrior_off_m => 2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 5
);
$warrior2 = array(
Battle::warrior_name => "Kobold",
Battle::warrior_group => "Team A",
Battle::warrior_hp => 40,
Battle::warrior_off => 12,
Battle::warrior_off_m => 2,
Battle::warrior_def => 6,
Battle::warrior_def_m => 3
);
$warrior3 = array(
Battle::warrior_name => "Elf",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 100,
Battle::warrior_off => 5,
Battle::warrior_off_m => 4,
Battle::warrior_def => 10,
Battle::warrior_def_m => 3
);
$warrior4 = array(
Battle::warrior_name => "Zwerg",
Battle::warrior_group => "Team B",
Battle::warrior_hp => 30,
Battle::warrior_off => 30,
Battle::warrior_off_m => 3,
Battle::warrior_def => 7,
Battle::warrior_def_m => 2
);
$warrior5 = array(
Battle::warrior_name => "Vampir",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 50,
Battle::warrior_off => 15,
Battle::warrior_off_m => 5,
Battle::warrior_def => 3,
Battle::warrior_def_m => 1
);
$warrior6 = array(
Battle::warrior_name => "Drache",
Battle::warrior_group => "Team C",
Battle::warrior_hp => 140,
Battle::warrior_off => 10,
Battle::warrior_off_m => 2,
Battle::warrior_def => 5,
Battle::warrior_def_m => 3
);
$battle -> add_group("Team A", Battle::blue);
$battle -> add_warrior($warrior1);
$battle -> add_warrior($warrior2);
$battle -> add_group("Team B", Battle::red);
$battle -> add_warrior($warrior3);
$battle -> add_warrior($warrior4);
$battle -> add_group("Team C", Battle::green);
$battle -> add_warrior($warrior5);
$battle -> add_warrior($warrior6);
$battle -> start(false, false, 1);
$battle -> debug($battle -> getLog());
|
|
| Zitat: |
Tipp:
Um Logausgaben zu verändern, empfiehlt es sich für Fortgeschrittene in der Methode ->debug() nachzuschauen.
|
Viel Spaß
Fragen können in diesem Thread gestellt werden
euer Maya
... Entschuldigung um Rechtschreib und Grammatikfehler
Kommentare
Zurück zur Übersicht
Autor Maya
|
|
|
|