using System;
using System.Windows.Forms; // Application DoEvents
using DxVBLib;
namespace MERCWars
{
///
/// Local application data. Used to create the level and move the units.
///
public class DirectXApp
{
static int Computer_units = 8;
static int Player_units = 4;
private DirectXWrapper DX = new DirectXWrapper();
private DirectXBitmapSurface DX_overlay = new DirectXBitmapSurface();
private unit Unit = new unit (Player_units,100,200);
private unit CUnit = new unit (Computer_units,450,200);
private background Halo = new background(50,50);
private DirectSoundClass DirectSound = new DirectSoundClass();
private background L1 = new background(800,600);
private background L2 = new background(800,600);
private background L3 = new background(800,600);
private background Intro = new background(800,600);
private background goodbye = new background(800,600);
//private DirectSoundClass.Sound sound_blast1 = new DirectSoundClass.Sound(DSClass, fn_Blast_1);
//private DirectSoundClass.Sound sound_bump = new DirectSoundClass.Sound(DSClass, fn_bump_1);
bool lost = true;
private int Score = 0;
//int Wait_Time = 15; // pause time for score screen
int player_units = Player_units; // local var for # player units
int computer_units = Computer_units;
Random random = new Random ((int) DateTime.Now.Ticks);
// ----- sound file type declarations
private DirectSoundClass.Sound sound_blast1;
private DirectSoundClass.Sound sound_bump;
private DirectSoundClass.Sound sound_goodatt1;
private DirectSoundClass.Sound sound_goodatt2;
private DirectSoundClass.Sound sound_death;
private DirectSoundClass.Sound sound_goodbye;
private DirectSoundClass.Sound sound_init;
private DirectSoundClass.Sound sound_warcry;
///
/// Windows HWND
///
private int handle;
public int Handle
{
set { handle = value; }
get { return handle; }
}
private int screenwidth = 640; // default values
public int ScreenWidth
{
get { return screenwidth; }
set { screenwidth = value; }
}
private int screenheight = 480;
public int ScreenHeight
{
get { return screenheight; }
set { screenheight = value; }
}
private int screenbits = 16;
public int ScreenBits
{
get { return screenbits; }
set { screenbits = value; }
}
private bool running = false;
public bool IsRunning
{
get { return running; }
}
public DirectXApp()
{
//
// TODO: Add constructor logic here
//
}
public void Initialize(string Main_BG) // initialize this level
{
this.DX.InitializeDirectXFullScreen(this.handle,this.screenwidth,this.screenheight,this.screenbits);
this.DX.InitializeDirectInput(this.handle);
DirectSound.Initialize(DX.directX, this.handle);
DX_overlay.Width = this.screenwidth;
DX_overlay.Height = this.screenheight;
DX_overlay.Initialize(DX.DirectDraw, Main_BG);
// player units
Unit.InitialiseSurface (DX.DirectDraw, 0, "US_Hum.bmp");
Unit.InitialiseSurface (DX.DirectDraw, 1, "war_intro2.bmp");
Unit.InitialiseSurface (DX.DirectDraw, 2, "IDF.bmp");
Unit.InitialiseSurface (DX.DirectDraw, 3, "US_Hum.bmp");
// computer units
CUnit.InitialiseSurface (DX.DirectDraw, 0 , "2s1-line.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 1 , "btr80.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 2 , "T-80U-3.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 3 , "tank.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 4 , "2s1-line.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 5 , "btr80.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 6 , "T-80U-3.bmp");
CUnit.InitialiseSurface (DX.DirectDraw, 7 , "tank.bmp");
// BG
Halo.InitialiseSurface (DX.DirectDraw, "halo.bmp");
L1.InitialiseSurface (DX.DirectDraw, "Level1.bmp");
L2.InitialiseSurface (DX.DirectDraw, "Level2.bmp");
L3.InitialiseSurface (DX.DirectDraw, "Level3.bmp");
Intro.InitialiseSurface (DX.DirectDraw, "intro_screen.bmp");
goodbye.InitialiseSurface (DX.DirectDraw, "goodbye.bmp");
//play_test = true;
//--- starts the main sound handler??
DX.directX.DirectSoundCreate("");
//----- sound file wav linkups
string fn_bump_1 = "ModernArmourRun.wav";
string fn_blast_1 = "Blaster1.wav";
string fn_init = "init.wav";
string fn_death = "bump_1.wav";
string fn_goodatt1 = "ModernArmourAttackGun.wav";
string fn_goodatt2 = "BomberAtckGun.wav";
string fn_warcry = "warcry.wav";
string fn_goodbye = "goodbye.wav";
sound_bump = new DirectSoundClass.Sound(DirectSound, fn_bump_1);
sound_blast1 = new DirectSoundClass.Sound(DirectSound, fn_blast_1);
sound_init = new DirectSoundClass.Sound(DirectSound, fn_init);
sound_death = new DirectSoundClass.Sound(DirectSound, fn_death);
sound_goodatt1 = new DirectSoundClass.Sound(DirectSound, fn_goodatt1);
sound_goodatt2 = new DirectSoundClass.Sound(DirectSound, fn_goodatt2);
sound_warcry = new DirectSoundClass.Sound(DirectSound, fn_warcry);
sound_goodbye = new DirectSoundClass.Sound(DirectSound, fn_goodbye);
}
public void StartApplication()
{
this.running = true;
MainLoop();
}
public void StopApplication()
{
this.running = false;
}
/// ///////////////////////////// Main fun times /////////////////////////////////////////////
private bool test;
private bool playing;
private bool read = false;
private int GetTickCount()
{
return System.Environment.TickCount;
}
private void level1 ()
{ DX.RepaintBackBuffer();
L1.BltFast (DX.DD_BackbufferSurface);
DX.FlipBackBufferAndPrimary();
pause();
Unit[0].Status = true;
Unit[0].XPosition = 100+(random.Next(0,1)*50);
Unit[0].YPosition = random.Next(2,5)*50;
Unit[1].Status = true;
Unit[1].XPosition = 100+random.Next(0,1)*50;
Unit[1].YPosition = random.Next(5,8)*50;
CUnit[0].Status = true;
CUnit[0].XPosition = 450-random.Next(0,1)*50;
CUnit[0].YPosition = random.Next(2,4)*50;
CUnit[1].Status = true;
CUnit[1].XPosition = 450-random.Next(0,1)*50;
CUnit[1].YPosition = random.Next(5,8)*50;
}
private void level2 ()
{ DX.RepaintBackBuffer();
L2.BltFast (DX.DD_BackbufferSurface);
DX.FlipBackBufferAndPrimary();
pause();
Unit[0].Status = true;
Unit[1].Status = true;
Unit[2].Status = true;
CUnit[0].Status = true;
CUnit[1].Status = true;
CUnit[2].Status = true;
CUnit[3].Status = true;
Unit[0].XPosition = 100+(random.Next(0,1)*50);
Unit[0].YPosition = random.Next(2,4)*50;
Unit[1].XPosition = 100+random.Next(0,1)*50;
Unit[1].YPosition = random.Next(5,7)*50;
Unit[2].XPosition = 100+(random.Next(0,1)*50);
Unit[2].YPosition = random.Next(7,9)*50;
CUnit[0].XPosition = 450-random.Next(0,2)*50;
CUnit[0].YPosition = random.Next(2,4)*50;
CUnit[1].XPosition = 450-random.Next(0,2)*50;
CUnit[1].YPosition = random.Next(3,6)*50;
CUnit[2].XPosition = 450-random.Next(0,2)*50;
CUnit[2].YPosition = random.Next(5,7)*50;
CUnit[3].XPosition = 450-random.Next(0,2)*50;
CUnit[3].YPosition = random.Next(7,9)*50;
}
private void level3 ()
{ DX.RepaintBackBuffer();
L3.BltFast (DX.DD_BackbufferSurface);
DX.FlipBackBufferAndPrimary();
pause();
Unit[0].Status = true;
Unit[1].Status = true;
Unit[2].Status = true;
Unit[3].Status = true;
CUnit[0].Status = true;
CUnit[1].Status = true;
CUnit[2].Status = true;
CUnit[3].Status = true;
CUnit[4].Status = true;
CUnit[5].Status = true;
CUnit[6].Status = true;
CUnit[7].Status = true;
Unit[0].XPosition = 100+(random.Next(0,2)*50);
Unit[0].YPosition = random.Next(2,7)*50;
Unit[1].XPosition = 100+random.Next(0,2)*50;
Unit[1].YPosition = random.Next(4,9)*50;
Unit[2].XPosition = 100+(random.Next(0,2)*50);
Unit[2].YPosition = random.Next(2,7)*50;
Unit[2].XPosition = 100+(random.Next(0,2)*50);
Unit[2].YPosition = random.Next(4,9)*50;
CUnit[0].XPosition = 450-random.Next(0,2)*50;
CUnit[0].YPosition = random.Next(2,6)*50;
CUnit[1].XPosition = 450-random.Next(0,2)*50;
CUnit[1].YPosition = random.Next(2,6)*50;
CUnit[2].XPosition = 450-random.Next(0,2)*50;
CUnit[2].YPosition = random.Next(2,6)*50;
CUnit[3].XPosition = 450-random.Next(0,2)*50;
CUnit[3].YPosition = random.Next(2,6)*50;
CUnit[4].XPosition = 450-random.Next(0,2)*50;
CUnit[4].YPosition = random.Next(5,9)*50;
CUnit[5].XPosition = 450-random.Next(0,2)*50;
CUnit[5].YPosition = random.Next(5,9)*50;
CUnit[6].XPosition = 450-random.Next(0,2)*50;
CUnit[6].YPosition = random.Next(5,9)*50;
CUnit[7].XPosition = 450-random.Next(0,2)*50;
CUnit[7].YPosition = random.Next(5,9)*50;
}
private void MainLoop()
{
// start intro screen
DX.RepaintBackBuffer();
Intro.BltFast (DX.DD_BackbufferSurface);
DX.FlipBackBufferAndPrimary();
sound_init.PlaySound(false);
pause();
test = false;
lost = false;
playing = true;
player_units = 2;
computer_units = 2;
Draw_Frame ();
//---- start level 1
level1(); // loads level conditions
while (playing == true)
{ // start inner while loop
Check_End_Of_Game();
if (end_conditions() == true)
{break;}
player_turn();
computer_turn ();
Draw_Frame();
end_of_turn ();
//do windows events
Application.DoEvents();
} // end of outer while loop
//----------------------------------------------------------------------------------------
player_units =3;
computer_units =4;
level2();
test = false;
playing = true;
read = false;
Draw_Frame ();
while (playing == true)
{
Check_End_Of_Game();
if (end_conditions() == true)
{break;}
player_turn();
computer_turn ();
Draw_Frame();
end_of_turn ();
//do windows events
Application.DoEvents();
} // end of outer while loop
player_units =4;
computer_units =8;
level3();
test = false;
playing = true;
read = false;
Draw_Frame ();
while (playing == true)
{
Check_End_Of_Game();
if (end_conditions() == true)
{break;}
player_turn();
computer_turn ();
Draw_Frame();
end_of_turn ();
//do windows events
Application.DoEvents();
} // end of outer while loop
DX.RepaintBackBuffer();
goodbye.BltFast (DX.DD_BackbufferSurface);
DX.FlipBackBufferAndPrimary();
pause ();
Application.Exit();
} // END MAIN GAME LOOP!!!!!
private void end_of_turn ()
{
while (playing == true) // allows quick response to user exit request when in player_turn
{
DX.DD_PrimarySurface.SetFontBackColor(0);
DX.DD_PrimarySurface.SetForeColor(255);
DX.DD_PrimarySurface.DrawText(200,500,"Press Enter to Continue",false);
while (test == false)
{
this.DX.DirectInput.Acquire();
this.DX.DirectInput.UpdateKeyBoardState();
if ( DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_RETURN ]==0x80)
{
this.DX.DirectInput.QuitKeyDown();
this.DX.DirectInput.Unacquire();
Draw_Frame ();
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_ESCAPE ]==0x80)
{
this.DX.DirectInput.QuitKeyDown();
this.DX.DirectInput.Unacquire();
Draw_Frame ();
playing = false;
break;
}
}
break; // gets out of the quick user response loop
} // end of inner while loop
}
private bool end_conditions ()
{
if (playing == false)
{
if (lost == true)
{
DX.RepaintBackBuffer();
DX.DD_BackbufferSurface.SetForeColor(255);
DX.DD_BackbufferSurface.DrawText (100,100, "YOU HAVE LOST THE MATCH", false);
DX.DD_BackbufferSurface.DrawText(100,130, "Your score is " + Score.ToString(),false);
DX.FlipBackBufferAndPrimary();
}
else
{
DX.RepaintBackBuffer();
DX.DD_BackbufferSurface.SetForeColor(255);
DX.DD_BackbufferSurface.DrawText (100,100, "YOU HAVE WON THE MATCH", false);
DX.DD_BackbufferSurface.DrawText(100,130, "Your score is " + Score.ToString(), false);
DX.FlipBackBufferAndPrimary();
}
pause();
return true;
} // end of game loop check
else
{return false;}
}
private void pause ()
{ playing = true;
test= false;
while (playing == true) // allows quick response to user exit request when in player_turn
{
DX.DD_PrimarySurface.SetFontBackColor(0);
DX.DD_PrimarySurface.SetForeColor(255);
DX.DD_PrimarySurface.DrawText(150,500,"Press Enter to Continue",false);
while (test == false)
{
this.DX.DirectInput.Acquire();
this.DX.DirectInput.UpdateKeyBoardState();
if ( DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_RETURN ]==0x80)
{
this.DX.DirectInput.QuitKeyDown();
this.DX.DirectInput.Unacquire();
Draw_Frame ();
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_ESCAPE ]==0x80)
{
this.DX.DirectInput.QuitKeyDown();
this.DX.DirectInput.Unacquire();
Draw_Frame ();
playing = false;
break;
}
}
break;
}
}
private void Draw_Frame ()
{
// Clear BackBuffer
DX.RepaintBackBuffer();
// Blit Background Picture
DX_overlay.BltFast( DX.DD_BackbufferSurface,0,0);
DX.Draw_Grid();
Unit.BltFast (DX.DD_BackbufferSurface);
CUnit.BltFast(DX.DD_BackbufferSurface);
DX.DD_BackbufferSurface.DrawText(450,30, "Your score is : " + Score.ToString(), false);
DX.DD_BackbufferSurface.DrawText(10,30,"Press ESC at anytime to QUIT",false);
// Flip the backbuffer surface to primary surface
DX.FlipBackBufferAndPrimary();
}
private void Draw_Frame_Player ()
{
// Clear BackBuffer
DX.RepaintBackBuffer();
// Blit Background Picture
DX_overlay.BltFast( DX.DD_BackbufferSurface,0,0);
DX.Draw_Grid();
Unit.BltFast (DX.DD_BackbufferSurface);
CUnit.BltFast(DX.DD_BackbufferSurface);
Halo.BltFast (DX.DD_BackbufferSurface);
DX.DD_BackbufferSurface.DrawText(250,30, "Your score is : " + Score.ToString(), false);
DX.DD_BackbufferSurface.DrawText(10,30,"Press ESC at anytime to QUIT",false);
// Flip the backbuffer surface to primary surface
DX.FlipBackBufferAndPrimary();
}
private void player_turn ()
{
for (int i=0; i < player_units; i++)
{
if (Unit[i].Status == true)
{
Halo.XPosition = Unit[i].XPosition;
Halo.YPosition = Unit[i].YPosition;
Draw_Frame_Player();
KeyHandler (i);
if (playing == false)
{break;}
}
}
}
private bool key_test;
private void KeyHandler (int unit_num)
{
//
key_test = false;
while (key_test == false)
{
this.DX.DirectInput.Acquire();
this.DX.DirectInput.UpdateKeyBoardState();
if ( DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_ESCAPE ]==0x80
)
{
DX.DD_BackbufferSurface.DrawText(100,100,"Exit",false);
playing = false;
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_RIGHT ]==0x80)
{
Unit[unit_num].ShipMove = Unit[unit_num].R_Move;
Unit[unit_num].Move();
Unit[unit_num].ShipMove = -1;
Check_Combat (false, unit_num);
sound_bump.PlaySound(false);
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_UP ]==0x80)
{
Unit[unit_num].ShipMove = Unit[unit_num].U_Move;
Unit[unit_num].Move();
Unit[unit_num].ShipMove = -1;
Check_Combat (false, unit_num);
sound_goodatt2.PlaySound(false);
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_DOWN ]==0x80)
{
Unit[unit_num].ShipMove = Unit[unit_num].D_Move;
Unit[unit_num].Move();
Unit[unit_num].ShipMove = -1;
Check_Combat (false, unit_num);
sound_goodatt1.PlaySound(false);
break;
}
if (DX.DirectInput.KEYB_STATE.key[ (int)CONST_DIKEYFLAGS.DIK_LEFT ]==0x80)
{
Unit[unit_num].ShipMove = Unit[unit_num].L_Move;
Unit[unit_num].Move();
Unit[unit_num].ShipMove = -1;
sound_goodatt2.PlaySound(false);
Check_Combat (false, unit_num);
break;
}
}
this.DX.DirectInput.QuitKeyDown();
this.DX.DirectInput.Unacquire(); //otherwise it loops the keyboard input.
}
private int target;
private void computer_turn ()
{
for (int i=0; i resolves combat
// 2> switchs the lowers statues to off
if (comp_turn == true)
{
// its the computers turn so check this against all player units
for (int i=0; i < player_units; i++)
{
if (CUnit[issuing_unit].Status == true)
{
if (Unit[i].Status == true)
{
if (CUnit[issuing_unit].XPosition == Unit[i].XPosition)
{
if (CUnit[issuing_unit].YPosition == Unit[i].YPosition)
{
// they are in the same spot and both alive
//MessageBox.Show("!!!");
Resolve_Combat (issuing_unit, i, true);
sound_goodatt2.PlaySound(false);
} // end if
else
{
DX.DD_PrimarySurface.SetForeColor(80);
DX.DD_PrimarySurface.DrawText(100,530,"This" + CUnit[issuing_unit].YPosition.ToString(), false);
}
}
}
}
}// end for
}
else
{
// player turn
for (int i=0; i < computer_units; i++)
{
if ((Unit[issuing_unit].XPosition== CUnit[i].XPosition) &
(Unit[issuing_unit].YPosition == CUnit[i].YPosition) &
(Unit[issuing_unit].Status == true) &&
(CUnit[i].Status == true))
{
// they are in the same spot and both alive
Resolve_Combat (i,issuing_unit, false);
} // end if
}// end for
} // end if
}
private void Resolve_Combat (int comp_num,int player_num, bool COffense)
{
// check offense vs. defense + random. Keep winner. Kill loser
if (COffense == true)
{
sound_blast1.PlaySound(false);
if (CUnit[comp_num].Offense > (Unit[player_num].Defense + random.Next(-5, 5)))
{
// play unit dies
Unit[player_num].Dead();
sound_death.PlaySound(false);
Score -= 50;
}
else
{
CUnit[comp_num].Dead();
sound_warcry.PlaySound(false);
Score += 200;
}
}
else
{
if (CUnit[comp_num].Defense > (Unit[player_num].Offense + random.Next(-5, 5)))
{
// play unit dies
Unit[player_num].Dead();
sound_death.PlaySound(false);
Score -= 50;
}
else
{
//DX.DD_BackbufferSurface.SetForeColor(80);
//DX.DD_BackbufferSurface.DrawText(100,530,"You are dead", false);
CUnit[comp_num].Dead();
sound_warcry.PlaySound(false);
Score += 200;
}
}
}
private void Move_TO (int C, int U)
{
/// determines the best route to the enemy
/// goal is to minize the smaller difference (x or y) default is x
if (Unit[U].XPosition-CUnit[C].XPosition == 0)
{
// check the Y distances
if (Unit[U].YPosition-CUnit[C].YPosition > 50)
{
CUnit[C].ShipMove = CUnit[C].D_Move;
CUnit[C].Move();
}
else
{
CUnit[C].ShipMove = CUnit[C].U_Move;
CUnit[C].Move();
}
}
else
{
if (Unit[U].XPosition-CUnit[C].XPosition > 0)
{
CUnit[C].ShipMove = CUnit[C].R_Move;
CUnit[C].Move();
}
else
{
CUnit[C].ShipMove = CUnit[C].L_Move;
CUnit[C].Move();
}
}
} // end Move_TO
int default_target = 0;
int default_dist = 2000000000;
private int Find_Enemy (int comp_num)
{
// default target is the first player unit
for (int i=0;i < player_units; i++)
{
if (Unit[i].Status == true)
{
if (find_dist (comp_num, i) < default_dist)
{
default_target = i;
} // end if
}
} // end loop
return default_target;
}
private float find_dist (int comp, int player)
{
return Unit[player].XPosition*CUnit[comp].XPosition + Unit[player].YPosition*CUnit[comp].YPosition;
}
bool valid;
private void Check_End_Of_Game ()
{
// -- check for existance of player units
valid = false;
for (int i=0;i< player_units; i++)
{
if (Unit[i].Status == true)
{
valid = true;
}
}
if (valid == false)
{
playing = false;
lost = true;
}
// -- check for existance of computer units
valid = false;
for (int i=0;i< computer_units; i++)
{
if (CUnit[i].Status == true)
{
valid = true;
}
}
if (valid == false)
{
playing = false;
lost = false;
}
}
private void RestoreSurfaces()
{
while ( ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_NOEXCLUSIVEMODE )
| ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_WRONGMODE )
| ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_SURFACELOST )
)
{
//Invoke window events for application first !!!
Application.DoEvents();
// Restore Surfaces
DX.DirectDraw.RestoreAllSurfaces();
}
//do window events for application
Application.DoEvents();
}
public void ResumeApplication()
{
RestoreSurfaces();
StartApplication();
}
///
/// Checks if the DirectDraw surfaces are lost e.g. Need to be restored
/// Needed for Surface restoring after flipping to Windowsmode and Back
///
///
public bool SurfacesLost()
{
bool rBool = false;
if ( ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_NOEXCLUSIVEMODE )
| ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_WRONGMODE )
| ( DX.DirectDraw.TestCooperativeLevel() == (int)CONST_DDRAWERR.DDERR_SURFACELOST )
)
{
rBool = true;
}
return rBool;
}
public delegate void ExitHandler(object sender, EventArgs e);
public event ExitHandler OnExit;
protected virtual void OnChanged(EventArgs e)
{
if (OnExit != null)
OnExit(this, e);
}
}
}