Skip to content

Commit ce6a36f

Browse files
committed
Project upload
1 parent 7f1adb6 commit ce6a36f

26 files changed

+2407
-1
lines changed

Bullet.cs

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#region Using Statements
2+
using System;
3+
using System.Collections.Generic;
4+
using Microsoft.Xna.Framework;
5+
using Microsoft.Xna.Framework.Audio;
6+
using Microsoft.Xna.Framework.Content;
7+
using Microsoft.Xna.Framework.Graphics;
8+
using Microsoft.Xna.Framework.Input;
9+
using Microsoft.Xna.Framework.Storage;
10+
using Microsoft.Xna.Framework.GamerServices;
11+
#endregion
12+
13+
namespace Tanks
14+
{
15+
class Bullet
16+
{
17+
private Model bulletModel;
18+
private Matrix world = Matrix.Identity;
19+
private BasicEffect effect;
20+
private GraphicsDevice graphicsDevice;
21+
private Matrix[] boneTransforms;
22+
23+
private Tank tank;
24+
private Tank targetTank;
25+
private Map map;
26+
private Vector3 position, direction, orientation, velocity, gravity;
27+
private BoundingSphere boundingSphere;
28+
private float speed, yaw;
29+
private bool shoot = false;
30+
31+
/* Music / sound related
32+
private SoundEffect shootSound;
33+
private SoundEffectInstance shootSoundInstance;
34+
*/
35+
36+
public BoundingSphere BoundingSphere
37+
{
38+
get { return boundingSphere; }
39+
set { boundingSphere = value; }
40+
}
41+
42+
public Vector3 Position
43+
{
44+
get { return position; }
45+
set { position = value; }
46+
}
47+
48+
public Vector3 Orientation
49+
{
50+
get { return orientation; }
51+
set { orientation = value; }
52+
}
53+
54+
public Bullet(GraphicsDevice graphicsDevice, ContentManager contentManager, Tank tank, Tank targetTank, Map map)
55+
{
56+
/* Music / sound related
57+
shootSound = contentManager.Load<SoundEffect>("shootSound");
58+
shootSoundInstance = shootSound.CreateInstance();
59+
shootSoundInstance.Volume = 0.7f;
60+
*/
61+
62+
this.graphicsDevice = graphicsDevice;
63+
bulletModel = contentManager.Load<Model>("esfera");
64+
this.effect = map.Effect;
65+
this.tank = tank;
66+
this.targetTank = targetTank;
67+
this.map = map;
68+
69+
this.orientation = tank.TankOrientation;
70+
71+
boundingSphere = new BoundingSphere();
72+
boundingSphere.Radius = 0.8f;
73+
74+
speed = 0.2f;
75+
gravity = new Vector3(0, -9.8f, 0);
76+
77+
boneTransforms = new Matrix[bulletModel.Bones.Count];
78+
}
79+
80+
public void Update(GameTime gameTime)
81+
{
82+
if (Keyboard.GetState().IsKeyDown(Keys.Space) && shoot == false)
83+
{
84+
/* Music / sound related
85+
shootSoundInstance.Play();
86+
*/
87+
88+
direction = Vector3.Zero;
89+
90+
Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(tank.Yaw + tank.TurretRotationValue, tank.CannonRotationValue, 0);
91+
92+
direction = Vector3.Transform(new Vector3(0, 0, 1), rotationMatrix);
93+
direction.Normalize();
94+
95+
position = tank.Position + tank.TankOrientation * 1.7f + direction;
96+
97+
velocity = direction * 20;
98+
99+
bulletModel.Root.Transform = Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(position);
100+
boundingSphere.Center = position;
101+
shoot = true;
102+
}
103+
104+
if (shoot)
105+
{
106+
float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
107+
position += velocity * time;
108+
velocity += gravity * time;
109+
boundingSphere.Center = position;
110+
}
111+
else
112+
{
113+
boundingSphere.Center = Vector3.Zero;
114+
position = tank.Position;
115+
bulletModel.Root.Transform = Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(position);
116+
}
117+
118+
if ((position.X < 1f || position.X > map.Width - 1f || position.Z < 1f || position.Z > map.Height - 1f)
119+
|| position.Y < map.CalcSurfaceFollow(position, 0f).Y || targetTank.Collided)
120+
{
121+
shoot = false;
122+
boundingSphere.Center = Vector3.Zero;
123+
}
124+
}
125+
126+
public void UpdateP2(GameTime gameTime)
127+
{
128+
if (Keyboard.GetState().IsKeyDown(Keys.NumPad5) && shoot == false)
129+
{
130+
/* Music / sound related
131+
shootSound.Play();
132+
*/
133+
134+
direction = Vector3.Zero;
135+
136+
Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(tank.Yaw + tank.TurretRotationValue, tank.CannonRotationValue, 0);
137+
138+
direction = Vector3.Transform(new Vector3(0, 0, 1), rotationMatrix);
139+
direction.Normalize();
140+
141+
position = tank.Position + tank.TankOrientation * 1.7f + direction;
142+
143+
velocity = direction * 20;
144+
145+
bulletModel.Root.Transform = Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(position);
146+
147+
shoot = true;
148+
}
149+
150+
if (shoot)
151+
{
152+
float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
153+
position += velocity * time;
154+
velocity += gravity * time;
155+
boundingSphere.Center = position;
156+
}
157+
else
158+
{
159+
boundingSphere.Center = Vector3.Zero;
160+
position = tank.Position;
161+
bulletModel.Root.Transform = Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(position);
162+
}
163+
164+
if ((position.X < 1f || position.X > map.Width - 1f || position.Z < 1f || position.Z > map.Height - 1f) || position.Y < map.CalcSurfaceFollow(position, 0f).Y || targetTank.Collided)
165+
{
166+
shoot = false;
167+
boundingSphere.Center = Vector3.Zero;
168+
}
169+
}
170+
171+
public void Draw()
172+
{
173+
bulletModel.Root.Transform = world;
174+
bulletModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
175+
176+
Matrix worldMatrix = Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(position);
177+
178+
if (shoot)
179+
{
180+
foreach (ModelMesh mesh in bulletModel.Meshes)
181+
{
182+
foreach (BasicEffect effect in mesh.Effects)
183+
{
184+
effect.View = this.effect.View;
185+
effect.Projection = this.effect.Projection;
186+
effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
187+
effect.EnableDefaultLighting();
188+
effect.LightingEnabled = true;
189+
}
190+
191+
mesh.Draw();
192+
}
193+
}
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)