-
Notifications
You must be signed in to change notification settings - Fork 0
/
Target.cs
109 lines (94 loc) · 2.93 KB
/
Target.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ShootingGallery.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace ShootingGallery
{
public class Target : WorldSpaceEntity
{
private const int targetRadius = 45;
private const float _DEFAULTSCALE = .3f;
private float _scale;
private const double _TIMETOFULLSIZE = 3.0;
private double _time;
private Random rand;
public Target(Vector2 targetPosition) : base()
{
this._position = targetPosition;
this._sprite = (Sprite)Resources.Load("target");
this._scale = _DEFAULTSCALE;
this._time = 0f;
this.rand = new Random();
}
public override void Update(ref GameTime gameTime)
{
var mState = Mouse.GetState();
if (mState.LeftButton == ButtonState.Pressed)
{
float mouseTargetDist = Vector2.Distance(_position, mState.Position.ToVector2());
if (mouseTargetDist < targetRadius * _scale)
{
int score = CalculateScore();
ReportScore(score);
MoveRandomly();
Reset();
}
}
_time += gameTime.ElapsedGameTime.TotalSeconds;
_scale = (float)Math.MinMagnitude(_time / _TIMETOFULLSIZE,1);
}
private int CalculateScore()
{
int score;
if (_scale < .4f)
score = 10;
else if (_scale < 0.8f)
score = 5;
else
score = 1;
return score;
}
private void ReportScore(int score)
{
GameManager.AddScore(score);
new FloatingPopUpText(
"galleryFont",
score.ToString(),
this._position,
this._position + new Vector2(0, -20),
2f
);
}
private void MoveRandomly()
{
_position.X = rand.Next(targetRadius, Game1.WIDTH - targetRadius);
_position.Y = rand.Next(targetRadius, Game1.HEIGHT - targetRadius);
}
private void Reset()
{
_scale = _DEFAULTSCALE;
_time = 0;
}
public override void Render(ref SpriteBatch _spriteBatch)
{
_spriteBatch.Draw(_sprite.GetTexture2D(),
_position - (new Vector2(targetRadius, targetRadius)*_scale),
null,
Color.White,
0,
new Vector2(0, 0),
_scale,
SpriteEffects.None,
0f
);
;
}
}
}