/
Player.cs
134 lines (109 loc) · 4.51 KB
/
Player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Toolkit;
using Windows.UI.Input;
using Windows.UI.Core;
namespace Project
{
using SharpDX.Toolkit.Graphics;
using SharpDX.Toolkit.Input;
// Player class.
class Player : GameObject
{
//private float speed = 0.006f;
//private float projectileSpeed = 20;
private float projectileSpeed = 300;
private float scaling;
private const float size = 100;
private Matrix originalWorld;
private double angleY;
private double angleX;
public Player(LabGame game)
{
this.game = game;
type = GameObjectType.Player;
model = game.assets.GetModel("player", CreatePlayerModel);
collisionRadius = 10;
//pos = new SharpDX.Vector3(0, game.boundaryBottom + 1f, 0);
pos = new Vector3(0, -1, 0);
//pos = new SharpDX.Vector3(0, 10, 0);
BoundingSphere modelBounds = model.CalculateBounds();
scaling = size / modelBounds.Radius;
originalWorld = Matrix.Translation(-modelBounds.Center.X, -modelBounds.Center.Y, -modelBounds.Center.Z)
* Matrix.Scaling(scaling);
basicEffect = new BasicEffect(game.GraphicsDevice)
{
View = game.camera.View,
Projection = game.camera.Projection,
//World = Matrix.Identity
World = originalWorld
};
}
public Model CreatePlayerModel()
{
//return game.assets.CreateTexturedCube("player.png", 0.7f);
Model model = game.Content.Load<Model>("cannon");
BasicEffect.EnableDefaultLighting(model, true);
return model;
}
// Method to create projectile texture to give to newly created projectiles.
private Model CreatePlayerProjectileModel()
{
//return game.assets.CreateTexturedCube("player projectile.png", new Vector3(0.3f, 0.2f, 0.25f));
Model model = game.Content.Load<Model>("Sphere");
BasicEffect.EnableDefaultLighting(model, true);
return model;
}
// Shoot a projectile.
private void fire()
{
Vector3 target = new Vector3(game.camera.View.Column3.X,
game.camera.View.Column3.Y, game.camera.View.Column3.Z);
Vector3 x_axis = new Vector3(game.camera.View.Column1.X,
game.camera.View.Column1.Y, game.camera.View.Column1.Z);
Vector3 start_pos = -Vector3.Normalize(Vector3.Cross(target, x_axis));
Vector3 end_pos = 99 * target;
Vector3 vel = projectileSpeed * Vector3.Normalize(end_pos - start_pos);
game.Add(new Projectile(game,
game.assets.GetModel("player projectile", CreatePlayerProjectileModel),
start_pos,
vel,
GameObjectType.Enemy
));
}
// Frame update.
public override void Update(GameTime gameTime)
{
if (game.keyboardState.IsKeyDown(Keys.Space)) { fire(); }
// TASK 1: Determine velocity based on accelerometer reading
//pos.X += (float)game.accelerometerReading.AccelerationX;
// Keep within the boundaries.
//if (pos.X < game.boundaryLeft) { pos.X = game.boundaryLeft; }
//if (pos.X > game.boundaryRight) { pos.X = game.boundaryRight; }
angleX = game.camera.angleX;
angleY = game.camera.angleY;
Matrix viewModel = game.camera.View;
//basicEffect.World = originalWorld * Matrix.Translation(pos) * Matrix.Scaling(scaling)
// * Matrix.RotationX((float)angleX)*Matrix.RotationY((float)angleY);
basicEffect.World = originalWorld * Matrix.Translation(pos);
// * Matrix.RotationX((float)-angleX)*Matrix.RotationY((float)angleY);
}
// React to getting hit by an enemy bullet.
public void Hit()
{
game.Exit();
}
public override void Tapped(GestureRecognizer sender, TappedEventArgs args)
{
fire();
}
public override void OnManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs args)
{
// pos.X += (float)args.Delta.Translation.X / 100;
}
}
}