/
Player.cs
106 lines (95 loc) · 3.19 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
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;
using System.Diagnostics;
// Player class.
class Player : PhysicalObject
{
private float projectileSpeed = 10;
public Player(LabGame game)
{
this.game = game;
type = GameObjectType.Player;
myModel = game.assets.GetModel("boat.png", CreatePlayerModel);
pos = new SharpDX.Vector3(0, 0, -1);
this.locallight.lightCol = new Vector4(0f,0.15f,0.5f,1f);
this.locallight.lightPos = new Vector4(this.pos.X, this.pos.Y,this.pos.Z-2,1f);
game.addLight(this.locallight);
velocity = new Vector3(0, 0, 0);
acceleration = new Vector3(0, 0, 0);
hitpoints = (int)(100 / game.difficulty);
armour = 1;
maxspeed = 0.5f;
maxaccel = 1.4f;
GetParamsFromModel();
}
/// <summary>
/// Create player model to render and use.
/// </summary>
/// <returns></returns>
public MyModel CreatePlayerModel()
{
return game.assets.CreateShip("boat.png");
}
/// <summary>
/// Frame update.
/// </summary>
/// <param name="gameTime">Time since last update.</param>
public override void Update(GameTime gameTime)
{
//System.Diagnostics.Debug.WriteLine(gameTime.ElapsedGameTime.Milliseconds);
if (hitpoints <= 0)
{
game.gameOver = true;
}
//Read acceleration from accelerometers
acceleration.X = (float)game.accelerometerReading.AccelerationX * 1.25f;
acceleration.Y = (float)game.accelerometerReading.AccelerationY * 1.25f;
//Update player physics
physicsUpdate(gameTime);
//Update following light
updateLight();
//Perform visual transformations
transform();
}
/// <summary>
/// Initiate event upon recieving a tap input.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public override void Tapped(GestureRecognizer sender, TappedEventArgs args)
{
fire(new Vector2((float)args.Position.X, (float)args.Position.Y));
}
// Initiate event upon recieving a swipe input
public override void OnManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs args)
{
//Do nothing
}
private void fire(Vector2 dir)
{
Vector3 direction = new Vector3(dir.X - 1542 / 2, -dir.Y + 1024 / 2, 0);
direction.Normalize();
game.Add(new Projectile(game,
game.assets.GetModel("shot", CreatePlayerProjectile),
pos,
direction * projectileSpeed,
this
));
}
public MyModel CreatePlayerProjectile()
{
return game.assets.CreateCannonBall();
}
}
}