class Sound { Vector2D m_Position; Vector2D m_Direction; boolean m_GhettoReflection; int m_Life; boolean m_Used; int m_SoundType; //0 - enemy //1 - player //2 - scream void Initialize() { m_Position = new Vector2D(); m_Direction = new Vector2D(); m_Used = false; m_GhettoReflection = false; m_Life = 0; m_SoundType = 0; } void Update() { float tempPosX = m_Position.x; float tempPosY = m_Position.y; if( m_GhettoReflection ) { tempPosX += m_Direction.x; } else { tempPosY += m_Direction.y; } //compute projected grid square int curCellIndexX = (int)(tempPosX / GRID.m_CellWidth); int curCellIndexY = (int)(tempPosY / GRID.m_CellWidth); boolean bounce1 = (tempPosX < 0.f || curCellIndexX > GRID.m_NumCellsX - 1); boolean bounce2 = (tempPosY < 0.f || curCellIndexY > GRID.m_NumCellsY - 1); curCellIndexX = CapInt( curCellIndexX, 0, GRID.m_NumCellsX - 1 ); curCellIndexY = CapInt( curCellIndexY, 0, GRID.m_NumCellsY - 1 ); if( GRID.m_Cells[ curCellIndexY * GRID.m_NumCellsX + curCellIndexX ].m_Wall || bounce1 || bounce2 ) { if( GRID.m_Cells[ curCellIndexY * GRID.m_NumCellsX + curCellIndexX ].m_Breakable && m_SoundType == 2 ) { m_Used = false; GRID.m_Cells[ curCellIndexY * GRID.m_NumCellsX + curCellIndexX ].m_Wall = false; return; } if( m_GhettoReflection ) { m_Direction.x = -m_Direction.x; } else { m_Direction.y = -m_Direction.y; } } else { m_Position.x = tempPosX; m_Position.y = tempPosY; } m_GhettoReflection ^= true; //check for sound hitting enemy if( m_SoundType != 0 ) { int i; for( i = 0; i < NUM_VISIBLE_ENEMIES; ++i ) { if( VISIBLE_ENEMIES[i].m_GridSquareX == curCellIndexX && VISIBLE_ENEMIES[i].m_GridSquareY == curCellIndexY ) { if( m_SoundType == 1 ) { VISIBLE_ENEMIES[i].SetExcited(); } else if( m_SoundType == 2 ) { VISIBLE_ENEMIES[i].SetFrozen(); } } } for( i = 0; i < NUM_INVISIBLE_ENEMIES; ++i ) { if( INVISIBLE_ENEMIES[i].m_GridSquareX == curCellIndexX && INVISIBLE_ENEMIES[i].m_GridSquareY == curCellIndexY ) { if( m_SoundType == 1 ) { INVISIBLE_ENEMIES[i].SetExcited(); } else if( m_SoundType == 2 ) { INVISIBLE_ENEMIES[i].SetFrozen(); } } } } //decrement life --m_Life; if( m_Life < 0 ) { m_Used = false; } } void Draw() { //only draw if we're in viewing distance //Vector2D thisPlayerToCurrent = new Vector2D( PLAYER[CURRENT_PLAYER].m_Position.x - m_Position.x, // PLAYER[CURRENT_PLAYER].m_Position.y - m_Position.y ); //if( !DRAW_ALL && thisPlayerToCurrent.MagnitudeSquared() > PLAYER[CURRENT_PLAYER].m_Hearing && m_SoundType != 2 ) Vector2D thisPlayerToCurrent = new Vector2D( PLAYER[1].m_Position.x - m_Position.x, PLAYER[1].m_Position.y - m_Position.y ); if( !DRAW_ALL && thisPlayerToCurrent.MagnitudeSquared() > PLAYER[1].m_Hearing && m_SoundType != 2 ) { return; } switch( m_SoundType ) { case 0: stroke( 0, 0, 255 ); fill( 0, 0, 255, 192 ); break; case 1: stroke( 255, 255, 255 ); fill( 255, 255, 255, 192 ); break; case 2: stroke( 255, 0, 0 ); fill( 255, 0, 0, 192 ); break; } float soundSize = m_Life * .25f; soundSize = CapFloat( soundSize, 1.f, 5.f ); ellipse( m_Position.x, m_Position.y, soundSize, soundSize ); } } class ShockWave { Vector2D m_Position; float m_Size; boolean m_Used; int m_SoundType; //0 - enemy //1 - player //2 - scream void Initialize() { m_Position = new Vector2D(); m_Used = false; m_Size = 0.f; m_SoundType = 0; } void Update() { m_Size += 3.f; if( m_Size > 40.f ) { m_Used = false; } } void Draw() { //only draw if we're in viewing distance //Vector2D thisPlayerToCurrent = new Vector2D( PLAYER[CURRENT_PLAYER].m_Position.x - m_Position.x, // PLAYER[CURRENT_PLAYER].m_Position.y - m_Position.y ); Vector2D thisPlayerToCurrent = new Vector2D( PLAYER[1].m_Position.x - m_Position.x, PLAYER[1].m_Position.y - m_Position.y ); float distance = thisPlayerToCurrent.MagnitudeSquared() - m_Size*m_Size; //if( !DRAW_ALL && distance > PLAYER[CURRENT_PLAYER].m_Hearing && m_SoundType != 2 ) if( !DRAW_ALL && distance > PLAYER[1].m_Hearing && m_SoundType != 2 ) { return; } switch( m_SoundType ) { case 0: stroke( 0, 0, 255 ); noFill(); break; case 1: stroke( 255, 255, 255 ); noFill(); break; case 2: stroke( 255, 0, 0 ); noFill(); break; } ellipse( m_Position.x, m_Position.y, m_Size, m_Size ); } } class SoundManager { Sound [] Sounds; ShockWave [] ShockWaves; void Initialize() { int i; Sounds = new Sound[NUM_SOUNDS]; for( i = 0; i < NUM_SOUNDS; ++i ) { Sounds[i] = new Sound(); Sounds[i].Initialize(); } ShockWaves = new ShockWave[NUM_SOUNDS]; for( i = 0; i < NUM_SOUNDS; ++i ) { ShockWaves[i] = new ShockWave(); ShockWaves[i].Initialize(); } } void Update() { int i; for( i = 0; i < NUM_SOUNDS; ++i ) { if( Sounds[i].m_Used ) { Sounds[i].Update(); } } for( i = 0; i < NUM_SOUNDS; ++i ) { if( ShockWaves[i].m_Used ) { ShockWaves[i].Update(); } } } void Draw() { int i; for( i = 0; i < NUM_SOUNDS; ++i ) { if( Sounds[i].m_Used ) { Sounds[i].Draw(); } } for( i = 0; i < NUM_SOUNDS; ++i ) { if( ShockWaves[i].m_Used ) { ShockWaves[i].Draw(); } } } void AddNewSound( float PosX, float PosY, float DirX, float DirY, int Life, int SoundType ) { int i; for( i = 0; i < NUM_SOUNDS; ++i ) { if( !Sounds[i].m_Used ) { Sounds[i].m_Used = true; Sounds[i].m_Position.Set( PosX, PosY ); Sounds[i].m_Direction.Set( DirX, DirY ); Sounds[i].m_Life = Life; Sounds[i].m_SoundType = SoundType; break; } } } void AddSoundBurst( float PosX, float PosY, int Life, int SoundType ) { float speed = 3.f; AddNewSound( PosX, PosY, 1.f * speed, 0.f, Life, SoundType ); AddNewSound( PosX, PosY, -1.f * speed, 0.f, Life, SoundType ); AddNewSound( PosX, PosY, 0.f, 1.f * speed, Life, SoundType ); AddNewSound( PosX, PosY, 0.f, -1.f * speed, Life, SoundType ); AddNewSound( PosX, PosY, .7f * speed, .7f * speed, Life, SoundType ); AddNewSound( PosX, PosY, -.7f * speed, .7f * speed, Life, SoundType ); AddNewSound( PosX, PosY, .7f * speed, -.7f * speed, Life, SoundType ); AddNewSound( PosX, PosY, -.7f * speed, -.7f * speed, Life, SoundType ); int i; for( i = 0; i < NUM_SOUNDS; ++i ) { if( !ShockWaves[i].m_Used ) { ShockWaves[i].m_Used = true; ShockWaves[i].m_Position.Set( PosX, PosY ); ShockWaves[i].m_Size = 0.f; ShockWaves[i].m_SoundType = SoundType; return; } } } }