-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit.cs
90 lines (76 loc) · 2.05 KB
/
Unit.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
using System;
class Unit
{
public static readonly int DOT = 8;
public static readonly int DTH = DOT / 2;
protected float mX, mY;
protected float mDX, mDY;
protected bool mGround = true;
public Unit( float x, float y, float dx, float dy )
{
mX = x;
mY = y;
mDX = dx;
mDY = dy;
}
public int getAngle4i( Unit u )
{
return( Util.GetAngle4i( u.mX - mX, u.mY - mY ) );
}
public bool isBlock()
{
return( Map.IsBlock( mX, mY ) );
}
public void jump( float dy )
{
mGround = false;
mDY = dy;
}
public void mightChangeDirection(Unit u)
{
if( u == this || !isCollision( u ) ){
return;
}
mDX = Math.Sign( mX - u.mX ) / 32.0f;
if( mDX == 0 ){
mDX = 1.0f / 32;
}
u.mDX = -mDX;
}
public virtual void step()
{
mX += mDX;
if( isBlock() ){
mDX = -mDX;
mX += mDX;
}
mY += mDY;
if( !mGround && mDY < 6.0f / DOT ){ // 重力
mDY += 1.0f / 32;
}
if( mDY < 0 && isBlock() ){ // 上昇中
if( !Map.IsBlock( mX + 1.0f / 16, mY ) ){
mX += 1.0f / 16;
}else if( !Map.IsBlock( mX - 1.0f / 16, mY ) ){
mX -= 1.0f / 16;
}else{ // 天井にぶつかる
mDY = 0;
mY = (int)( mY - 0.5f ) + 1.5f;
}
}
if( mDY > 0 && isBlock() ){ // 下降中
mDY = 0;
mY = (int)( mY - 0.5f ) + 0.5f;
mGround = true;
}
if( mGround && !Map.IsBlock( mX, mY + 1.0f / DOT ) ){ // 地面から落ちる
mGround = false;
mY += 1.0f / DOT;
mDY = 1.0f / 16;
}
}
public bool isCollision( Unit u )
{
return( Math.Abs( mX - u.mX ) < 6.0f / DOT && Math.Abs( mY - u.mY ) < 6.0f / DOT );
}
}