So yeah, I'm creating my first attempt at NPC Movement, obviously the code itself isn't going to be too great since I'm not that great but learning, so don't make fun of me. XP But yeah, i'm having problem regarding my onwall check, oddly I added an onwall check, and now the NPC isn't moving at all. Could anyone tell me why? Also, I know I could use move(), but i'm going to try with just adjusting the x/y first so any advice regarding a way to do it better than my current would be appreciated.
Here's the code:
PHP Code:
function onPlayerchats()
{
if ( player.chat == "/destroy" ) {
destroy();
}
}
function onCreated()
{
setshape(1, 32, 48);
initializeCharacter();
}
function initializeCharacter()
{
setcharAni("idle", nil);
this.charDirection = 2;
this.decisionRan = int(random(0, 2));
if ( this.decisionRan == 0 ) this.decisionAction = "Walk";
if ( this.decisionRan == 1 ) this.decisionAction = "Idle";
if ( this.decisionRan == 2 ) this.decisionAction = "Animate";
this.decisionTimer = 0;
this.decisionMax = int(random(2, 5));
onTimeout();
}
function onTimeout()
{
introduceDecisions();
introduceLife();
setTimer(0.01);
}
function introduceDecisions()
{
if ( this.decisionTimer < 5 ) {
this.decisionTimer += .05;
}
if ( this.decisionTimer >= this.decisionMax ) {
this.decisionRan = int(random(0, 3));
if ( this.decisionRan == 0 ) this.decisionAction = "Walk";
if ( this.decisionRan == 1 ) this.decisionAction = "Idle";
if ( this.decisionRan == 2 ) this.decisionAction = "Animate";
this.decisionTimer = 0;
this.charAni = false;
this.decisionMax = int(random(1, 4));
}
}
function introduceLife()
{
if ( this.decisionAction == "Walk" ) characterMove();
if ( this.decisionAction == "Idle" ) characterIdle();
if ( this.decisionAction == "Animate" ) characterAnimate();
dir = this.charDirection;
}
function characterMove()
{
if ( this.dirTimer < 1 ) this.dirTimer += .05;
if ( this.dirTimer >= 1 ) {
this.xRan = int(random(0, 3));
this.yRan = int(random(0, 3));
this.dirTimer = 0;
}
if ( this.xRan == 0 ) {
this.xMove = .45;
}
if ( this.xRan == 1 ) {
this.xMove = -.45;
}
if ( this.xRan == 2 ) this.xMove = 0;
if ( this.yRan == 0 ) {
this.yMove = .45;
}
if ( this.yRan == 1 ) {
this.yMove = -.45;
}
if ( this.yRan == 2 ) this.yMove = 0;
if ( this.xRan == 0 ) this.charDirection = 3;
else
if ( this.xRan == 1 ) this.charDirection = 1;
else
if ( this.yRan == 0 ) this.charDirection = 2;
else
if ( this.yRan == 1 ) this.charDirection = 0;
this.charMoving = true;
if ( this.charMoving ) {
this.charPos = {this.x + 1.5 + vecx(this.charDirection),
this.y + 2 + vecy(this.charDirection)};
chat = this.charPos;
if ( !onwall(this.charPos[0], this.charPos[1]) ) {
x = x + this.xMove;
y = y + this.yMove;
if ( !this.charAni ) {
setcharAni("walk", nil);
this.charAni = true;
}
}
}
}
function characterIdle()
{
this.charMoving = false;
setcharAni("idle", nil);
}
function characterAnimate() {
this.charMoving = false;
if ( !this.charAni ) {
setcharAni("lift", nil);
this.charAni = true;
}
}