Location: PHPKode > scripts > RPG Ajax Creator > fight.php
<?php
session_start ();
/**
* includes classes
*/
require_once ('global/inc.main.php');
/**
* instances classes PHP
*/
$rand = mt_rand (0, 2);
$hero = unserialize ($_SESSION['hero']);
$streum = array ();
for ($i = 0; $i <= $rand; $i ++) {
	$streum[$i] = new ogoblin ('char.gif', array ('attaque' => 3, 'defense' => 2, 'pv' => 20, 'init' => 2000));
}
$ajax = new oajax ();
if (isset ($_POST['pv'])) {
	$hero -> isHit ($_POST['pv']);
	if (false === $hero -> isDead) {
		foreach ($streum as $nb => $classe) {
			$hero -> xpGain ($classe -> iXP);
			$hero -> setTrophies ($classe -> sName);
		}
	}
	$_SESSION['hero'] = serialize ($hero);
}
/**
* déclarations classes js
* instances classes js
*/
echo <<<HTML
<script type="text/javascript">
window.opener.document.getElementById('nav').style.visibility='hidden';
var delay = 1;

function hero (initHero, attHero, defHero, pvHero, pvID, hitNB, healBT, timerID, hitInit, healInit) {
	this.initHero = initHero;
	this.attHero = attHero;
	this.defHero = defHero;
	this.pvHero = pvHero;
	this.pvID = pvID;
	this.hitNB = hitNB;
	this.healBT=healBT;
	this.timerDivID = timerID;
	this.living = hitNB + 1;
	this.hitInit = hitInit;
	this.healInit = healInit;
	this.constantLoss = 0;

}

hero.prototype.hitHero = function (oStreum) {
	with (hero) {
		var hit = (this.attHero+Math.round (Math.random())) - oStreum.defStreum;
		if (hit > 0) {

			oStreum.pvStreum -= hit;
		}
		document.getElementById(oStreum.pvID).innerHTML = oStreum.pvStreum;
		if (oStreum.pvStreum > 0 && this.pvHero > 0) {
			hero.StartTheTimerHero (this.initHero);
		} else if (oStreum.pvStreum <= 0) {
			oStreum.isDead = true;
			clearTimeout(oStreum.timerID);
			this.living -= 1;
			if (this.living > 0) {
				hero.StartTheTimerHero (this.initHero + this.hitInit);
			} else {
				window.opener.document.getElementById('nav').style.visibility='visible';
				window.opener.document.getElementById('fight').innerHTML='';
				updateHero ({$hero -> getCar('pv')} - this.pvHero);
				self.close ();
				clearTimeout(this.timerID);
			}
		}
	}
}

hero.prototype.healHero = function () {
	with (hero) {
		if (this.living > 0) {
			this.pvHero += Math.round (Math.random() * 10);
			if (this.pvHero > {$hero -> getCar ('pv')}) {
				this.pvHero = {$hero -> getCar ('pv')};
			}
			if (setPv = document.getElementById(this.pvID)) {
				setPv.innerHTML = this.pvHero;
			}
			hero.StartTheTimerHero (this.initHero + this.healInit);
		} else {
			if (pvHeal = document.getElementById(this.healBT)) {
				pvHeal.disabled=false;
			}
			window.opener.document.getElementById('nav').style.visibility='visible';
			window.opener.document.getElementById('fight').innerHTML='';
			updateHero ({$hero -> getCar('pv')} - this.pvHero);
			self.close ();
		}
	}
}

hero.prototype.StartTheTimerHero = function (secs) {
	with (hero) {
		document.getElementById(this.timerDivID).innerHTML = secs;
		if (secs<=0 || this.living <= 0)
			{
			this.pvHero -= this.constantLoss;
			if (this.pvHero <= 0) {
				updateHero ({$hero -> getCar('pv')} - this.pvHero);
				window.opener.location.href = 'death.php';
				self.close ();
			}
			document.getElementById(this.timerDivID).style.color='#ff0000';
			clearTimeout(this.timerID);
			for (i = 0; i <= this.hitNB; i++ ) {
				if (streum[i].isDead == false) {
					if (hitIt = document.getElementById('hitStreumId['+i+']')) {
						hitIt.disabled=false;
					}
				} else {
					if (hitIt = document.getElementById('hitStreumId['+i+']')) {
						hitIt.disabled=true;
					}
				}
			}
			if (this.living > 0) {
				if (pvHeal = document.getElementById(this.healBT)) {
					pvHeal.disabled=false;
				}
			} else {
			window.opener.document.getElementById('nav').style.visibility='visible';
			window.opener.document.getElementById('fight').innerHTML='';
			updateHero ({$hero -> getCar('pv')} - this.pvHero);
			self.close ();
			}
		}
		else if (this.living > 0) {
			if (this.pvHero <= 0) {
				updateHero ({$hero -> getCar('pv')} - this.pvHero);
				window.opener.location.href = 'death.php';
				self.close ();
			}
			document.getElementById(this.timerDivID).style.color='#000000';
			for (i = 0; i <= this.hitNB; i++ ) {
				if (hitIt = document.getElementById('hitStreumId['+i+']')) {
					hitIt.disabled=true;
				}
			}
			if (pvHeal = document.getElementById(this.healBT)) {
				pvHeal.disabled=true;
			}
			self.status = secs;
			secs = secs - 10;
			this.timerIDH = self.setTimeout("hero.StartTheTimerHero("+secs+")", delay);
		}
	}
}


function streum (initStreum, attStreum, defStreum, pvStreum, pvID, timerID, nb, hitInit, poison) {
	this.initStreum = initStreum;
	this.attStreum = attStreum;
	this.defStreum = defStreum;
	this.pvStreum = pvStreum;
	this.pvID = pvID;
	this.timerDivID = timerID;
	this.nb = nb;
	this.isDead = false;
	this.hitInit = hitInit;
	this.poison = poison;
	this.constantLoss = 0;
}

streum.prototype.hitStreum = function (oHero) {
	with (streum) {
		var hit = (this.attStreum+Math.round (Math.random())) - oHero.defHero;
		if (hit > 0) {
			oHero.pvHero -= hit;
			if (this.poison != false) {
				oHero.constantLoss += this.poison;
				document.getElementById('hero').style.borderColor='#00ff00';
			}
		}
		document.getElementById(oHero.pvID).innerHTML = oHero.pvHero;
		if (oHero.pvHero > 0 && this.pvStreum > 0) {
			streum[this.nb].StartTheTimerStreum(this.initStreum + this.hitInit);
		} else if (oHero.pvHero <= 0) {
			clearTimeout(oHero.timerID);
			updateHero ({$hero -> getCar('pv')} - this.pvHero);
			window.opener.location.href = 'death.php';
			self.close ();
		}
	}
}

streum.prototype.StartTheTimerStreum = function (secs) {
	with (streum) {
		document.getElementById(this.timerDivID).innerHTML = secs;
		if (secs<=0) {
			clearTimeout(this.timerIDS);
			this.hitStreum (hero);
		}
		else {
			self.status = secs;
			secs = secs - 10;
			this.timerID = self.setTimeout("streum["+this.nb+"].StartTheTimerStreum("+secs+")", delay);
		}
	}
}

function heal () {
	hero.healHero ();
}
function hit (clef) {
	hero.hitHero (streum[clef]);
}

var hero = new hero ({$hero -> getCar ('init')}, {$hero -> getCar ('attaque')}, {$hero -> getCar ('defense')}, {$hero -> getCar ('pv')}, 'pvHeroId', {$rand}, 'healHeroId', 'timerHeroId', {$hero -> getCost ('hit')}, {$hero -> getCost ('heal')});
HTML;
foreach ($streum as $clef => $val) {
	echo 'streum[',$clef,'] = new streum (', $streum[$clef] -> getCar ('init'),',',$streum[$clef] -> getCar ('attaque'),',',$streum[$clef] -> getCar ('defense'),',',$streum[$clef] -> getCar ('pv'),',\'pvStreumId[',$clef,']\', \'timerStreumId[',$clef,']\', ',$clef,',', $streum[$clef] -> getCost ('hit'),',', $poison = false !== $streum[$clef] -> getSpecialAbility('poison')?$streum[$clef] -> getSpecialAbility('poison'):0, ');';
}

/**
* création interface
*/
echo <<<HTML
</script>
<div id="arene">
	<div id="hero" style="position: absolute; border: 1px solid #000000; width: 100px;top: 10px; left: 10px; margin: 10px;">
		<img src="char/char2.gif"/>
HTML;
		if (true === $hero -> getOnFightCan('heal')) {
			echo <<<HTML
			<input type="button" id="healHeroId" value="heal" onclick="heal();" style="cursor: pointer;"/>
HTML;
		}
		echo <<<HTML
		<div id="pvHeroId" style="color: #000000; font-weight: bold;"></div>
		<div id="timerHeroId" style="font-style: italic; ">
		</div>
	</div>
	<div id="streums" style="position: absolute; border: 1px solid #000000; width: 100px;left: 120px; top: 10px; margin: 10px;">
HTML;
	foreach ($streum as $clef => $val) {
		echo <<<HTML
		<div id="streum[{$clef}]" style="margin: 5px; border-bottom: 1px solid #000000;">
		<img src="char/char.gif" />
HTML;
		if (true === $hero -> getOnFightCan('hit')) {
			echo <<<HTML
			<input type="button" id="hitStreumId[{$clef}]" value="hit" onclick="hit({$clef});" style="cursor: pointer;"/>
HTML;
		}
		echo <<<HTML
		<div id="pvStreumId[{$clef}]" style="color: #000000; font-weight: bold;"></div>
		<br />
		<div id="timerStreumId[{$clef}]" style="font-style: italic; ">
		</div>
		</div>
HTML;
	}
/**
* start the timers
*/
echo  <<<HTML
	</div>
</div>
<div id="over"></div>
<script type = "text/javascript">
document.getElementById('pvHeroId').innerHTML = hero.pvHero;
var nb = {$rand};
for (i = 0; i <= nb; i ++) {
	document.getElementById(streum[i].pvID).innerHTML = streum[i].pvStreum;
}

hero.StartTheTimerHero (hero.initHero);
for (i = 0; i <= nb; i ++) {
	streum[i].StartTheTimerStreum (streum[i].initStreum);
}


</script>
HTML;
Return current item: RPG Ajax Creator