/
Box.cs
88 lines (70 loc) · 1.75 KB
/
Box.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace spatial_hash
{
class Box : Body
{
public float width;
public float height;
public Color color = Color.White;
private float dirX;
private float dirY;
private float speed;
private Random rnd;
private int screenW;
private int screenH;
public Box(int _screenW, int _screenH, Random _rndRandom)
: base()
{
screenW = _screenW;
screenH = _screenH;
rnd = _rndRandom;
x = randomBetween(0, screenW);
y = randomBetween(0, screenH);
width = randomBetween(10, 50);
height = randomBetween(10, 50);
speed = randomBetween(50, 250);
dirX = rnd.NextFloat() < 0.5d ? -1 : 1;
dirY = rnd.NextFloat() < 0.5d ? -1 : 1;
aabb = new AABB();
updateAABB();
}
public void update(float dt)
{
// wander about
if (rnd.NextFloat() > 0.99)
{
dirX = -dirX;
speed = randomBetween(50, 250);
}
if (rnd.NextFloat() > 0.99)
dirY = -dirY;
// update position
x += (dirX * speed * dt);
y += dirY * speed * dt;
// bounds checks
if (x < 0 || x > screenW)
dirX = -dirX;
if (y < 0 || y > screenH)
dirY = -dirY;
updateAABB();
}
private void updateAABB()
{
aabb.min.X = x;
aabb.min.Y = y;
aabb.max.X = x + width;
aabb.max.Y = y + height;
}
/** Return a random float between 'from' and 'to', inclusive. */
public float randomBetween(int from, int to)
{
return rnd.Next(from, to);
//return from + ((to - from) * rnd.NextFloat());
}
}
}