class Player { boolean m_MovedThisFrame; int m_BlendInValue; int m_State; // 0 - hidden // 1 - waiting to pounce // 2 - ready to pounce // 3 - pouncing // 4 - hurt // 5 - moving // 6 - dead void Initialize() { m_State = 0; m_BlendInValue = 0; } void Update() { m_BlendInValue -= 2; //blend in faster if we're waiting to pounce if( m_State == 1 ) { m_BlendInValue -= 3; } if( m_BlendInValue < 0 ) { m_BlendInValue = 0; if( m_State == 1 || m_State == 2 ) { m_State = 2; } else { m_State = 0; } } } void Draw() { fill( m_BlendInValue ); ellipse( SHADOW_BITS[0].m_Position.x, SHADOW_BITS[0].m_Position.y, SHADOW_BITS[0].m_DrawWidth, SHADOW_BITS[0].m_DrawHeight ); switch( m_State ) { case 0: { stroke( 255 ); float eyeOffset = SHADOW_BITS[0].m_DrawWidth / 4.f; float x = SHADOW_BITS[0].m_Position.x; float y = SHADOW_BITS[0].m_Position.y; line( x - eyeOffset, y - eyeOffset, x - 2.f, y - 3.f ); line( x + eyeOffset, y - eyeOffset, x + 2.f, y - 3.f ); line( x - eyeOffset + 1.f, y + 3.f, x + eyeOffset - 1.f, y + 3.f ); point( x - eyeOffset, y + 2.f ); point( x + eyeOffset, y + 2.f ); break; } case 1: { stroke( 255 ); float x = SHADOW_BITS[0].m_Position.x; float y = SHADOW_BITS[0].m_Position.y; ellipse( x - 3.f, y - 3.f, 2.f, 2.f ); ellipse( x + 3.f, y - 3.f, 2.f, 2.f ); point( x - 2.f, y + 3.f ); break; } case 2: { stroke( 255 ); float eyeOffset = SHADOW_BITS[0].m_DrawWidth / 4.f; float x = SHADOW_BITS[0].m_Position.x; float y = SHADOW_BITS[0].m_Position.y; line( x - eyeOffset, y - eyeOffset, x - 2.f, y - 3.f ); line( x + eyeOffset, y - eyeOffset, x + 2.f, y - 3.f ); line( x - eyeOffset + 1.f, y + 3.f, x + eyeOffset - 1.f, y + 3.f ); point( x - eyeOffset, y + 2.f ); point( x + eyeOffset, y + 2.f ); break; } case 3: { stroke( 255 ); float eyeOffset = SHADOW_BITS[0].m_DrawWidth / 4.f; float x = SHADOW_BITS[0].m_Position.x; float y = SHADOW_BITS[0].m_Position.y; line( x - eyeOffset, y - 3.f, x - 2.f, y - eyeOffset ); line( x + eyeOffset, y - 3.f, x + 2.f, y - eyeOffset ); line( x - 4.f, y + 4.f, x - 2.f, y + 2.f ); line( x - 2.f, y + 2.f, x, y + 4.f ); line( x, y + 4.f, x + 2.f, y + 2.f ); line( x + 2.f, y + 2.f, x + 4.f, y + 4.f ); break; } case 4: case 5: { stroke( 255 ); float x = SHADOW_BITS[0].m_Position.x; float y = SHADOW_BITS[0].m_Position.y; ellipse( x - 3.f, y - 3.f, 2.f, 2.f ); ellipse( x + 3.f, y - 3.f, 2.f, 2.f ); point( x - 2.f, y + 3.f ); break; } case 6: { /* line( x - eyeOffset, y + 3.f, x + eyeOffset, y + 3.f ); line( x - eyeOffset + 1.f, y + 2.f, x - eyeOffset + 1.f, y + 4.f ); line( x, y + 2.f, x, y + 4.f ); line( x + eyeOffset - 1.f, y + 2.f, x + eyeOffset - 1.f, y + 4.f ); */ } } //draw pounce target if( m_State == 1 || m_State == 2 ) { Vector2D toPounceX = new Vector2D(); toPounceX.x = mouseX - SHADOW_BITS[0].m_Position.x; toPounceX.y = mouseY - SHADOW_BITS[0].m_Position.y; float toPounceDist = toPounceX.MagnitudeSquared(); if( toPounceDist > POUNCE_DIST_SQ ) { toPounceX.Normalize(); toPounceX.Multiply( POUNCE_DISTANCE ); } float xCenterX = SHADOW_BITS[0].m_Position.x + toPounceX.x; float xCenterY = SHADOW_BITS[0].m_Position.y + toPounceX.y; if( m_State == 1 ) { stroke( 255, 0, 0 ); } else { stroke( 0, 255, 0 ); } line( xCenterX - 3.f, xCenterY - 3.f, xCenterX + 3.f, xCenterY + 3.f ); line( xCenterX - 3.f, xCenterY + 3.f, xCenterX + 3.f, xCenterY - 3.f ); } } void Move( float moveX, float moveY ) { if( m_MovedThisFrame == false ) { //compute distance to the desired new position Vector2D move = new Vector2D(); move.x = moveX; move.y = moveY; float moveDistance = move.MagnitudeSquared(); if( moveDistance < MAX_MOVE_DISTANCE ) { SHADOW_BITS[0].m_Position.x += moveX; SHADOW_BITS[0].m_Position.y += moveY; } else { move.Normalize(); move.Multiply( MAX_MOVE_DISTANCE ); SHADOW_BITS[0].m_Position.x += move.x; SHADOW_BITS[0].m_Position.y += move.y; } m_MovedThisFrame = true; SetActive(); } } void SetActive() { m_BlendInValue = 200; m_State = 5; } void SetReadyToPounce() { m_State = 1; } void Pounce( float x, float y ) { //we have to be fully hidden to pounce if( m_BlendInValue != 0 ) { m_State = 5; return; } //compute distance to the desired new position Vector2D move = new Vector2D(); move.x = x - SHADOW_BITS[0].m_Position.x; move.y = y - SHADOW_BITS[0].m_Position.y; float moveDistance = move.MagnitudeSquared(); if( moveDistance < POUNCE_DIST_SQ ) { SHADOW_BITS[0].m_Position.x = x; SHADOW_BITS[0].m_Position.y = y; } else { move.Normalize(); move.Multiply( POUNCE_DISTANCE ); SHADOW_BITS[0].m_Position.x += move.x; SHADOW_BITS[0].m_Position.y += move.y; } CheckForRedKill(); m_BlendInValue = 200; m_State = 3; } void CheckForRedKill() { int i; for( i = 0; i < NUM_RED; ++i ) { if( RED[i].m_State != 2 ) { Vector2D usToRed = new Vector2D( RED[i].m_Position.x - SHADOW_BITS[0].m_Position.x, RED[i].m_Position.y - SHADOW_BITS[0].m_Position.y ); float magSquared = usToRed.MagnitudeSquared(); if( magSquared < RED[i].m_Width * RED[i].m_Width ) { RED[i].Die(); } } } } boolean IsHidden() { return (m_State != 0 && m_State != 2); } };