-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
185 lines (164 loc) · 6.7 KB
/
Enemy.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Chroma;
using Chroma.Graphics;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using static System.Math;
namespace Chromastein
{
class Enemy
{
public enum EnemyType
{
Soldier = 0,
Boss = 1
}
public static string AssetsFolder = "/Sprites/Enemies/";
public EnemyType ObjType;
public Spritesheet EnemySheet;
public float PosX, PosY;
public double Rotation;
public float Speed;
public float Size;
/// <summary>
/// Really should refactor this to be dynamic
/// </summary>
public int WalkFrames;
// Same thing
public int WalkTime;
/// <summary>
/// Animation state for the spritesheet
/// </summary>
public int EnemyState = 0;
public float ZIndex = 0;
private float oldSpeed;
public Enemy(int positionX, int positionY, EnemyType thisType)
{
ObjType = thisType;
PosY = positionX + 0.5f;
PosX = positionY + 0.5f;
}
public virtual void InitContent(string contentRoot)
{
contentRoot += AssetsFolder;
switch (ObjType)
{
case EnemyType.Soldier:
EnemySheet = new Spritesheet(contentRoot + "guard.png");
WalkFrames = 4;
WalkTime = 1000;
Speed = 1;
Size = 0.2f;
break;
default:
EnemySheet = new Spritesheet(contentRoot + "../fuckedup.png");
WalkFrames = 0;
WalkTime = 1000;
Speed = 0.1f;
Size = 0;
break;
}
oldSpeed = Speed;
EnemySheet.CellWidth = 64;
EnemySheet.CellHeight = 64;
}
public virtual void Draw(RenderContext context, Vector2 playerPos, Vector2 playerDir, Vector2 playerPlane, Vector2 screenSize)
{
// Distance from the sprite to the player
float dX = PosX - playerPos.X;
float dY = PosY - playerPos.Y;
// Transform sprite with the inverse camera matrix
// [ planeX dirX ] -1 [ dirY -dirX ]
// [ ] = 1/(planeX*dirY-dirX*planeY) * [ ]
// [ planeY dirY ] [ -planeY planeX ]
double invDet = 1.0 / (playerPlane.X * playerDir.Y - playerDir.X * playerPlane.Y); // Invert for correct matrix multiplication
double transformX = invDet * (playerDir.Y * dX - playerDir.X * dY);
double transformY = invDet * (-playerPlane.Y * dX + playerPlane.X * dY); // Screen depth, Z value
int spriteScreenX = (int)((screenSize.X / 2) * (1 + transformX / transformY));
// How big the sprite is in pixels
int size = Abs((int)(screenSize.Y / (transformY))); // Using 'transformY' instead of the real distance prevents fisheye
// How big the sprite is relative to its usual height since I have to use scale
double scale = (float)size / EnemySheet.Texture.Height;
int x = -size / 2 + spriteScreenX;
if (transformY > 0 && (x + EnemySheet.CellWidth * scale) > 0 && x < screenSize.X)
{
Vector2 TexPosition = new Vector2(x, -size / 2 + screenSize.Y / 2);
Vector2 TexScale = new Vector2((float)scale, (float)scale);
EnemySheet.Draw(context, EnemyState, TexPosition, TexScale, Vector2.Zero, 0);
}
}
public virtual void Update(float deltaTime, Vector2 playerPos)
{
float dX = playerPos.X - PosX;
float dY = playerPos.Y - PosY;
float dist = (float)Sqrt(dX * dX + dY * dY);
if (dist > 4)
{
double angle = Atan2(dY, dX);
Rotation = angle;
Speed = oldSpeed;
if(WalkFrames != 0)
EnemyState = (int)Floor(
(float)(DateTime.Now.Millisecond % WalkTime / (WalkTime / WalkFrames))) + 1;
MoveEnemy(deltaTime);
}
else
{
EnemyState = 0;
Speed = 0;
}
}
private void MoveEnemy(float deltaTime)
{
// Speed of player's movement
float moveSpeed = deltaTime * Speed; // The constant value is in squares/second
Vector2 newEnemyPos = new Vector2(PosX, PosY);
Vector2 enemyForward = new Vector2((float)Cos(Rotation), (float)Sin(Rotation));
Vector2 enemyRight = new Vector2((float)Sin(Rotation), (float)-Cos(Rotation));
newEnemyPos += enemyForward * moveSpeed;
Vector2 xDir = new Vector2(
newEnemyPos.X,
PosY);
Vector2 yDir = new Vector2(
PosX,
newEnemyPos.Y);
if (!CheckWallCollision(xDir) && !CheckSpriteCollision(xDir)) PosX = newEnemyPos.X;
if (!CheckWallCollision(yDir) && !CheckSpriteCollision(yDir)) PosY = newEnemyPos.Y;
}
private bool CheckWallCollision(Vector2 mapPos)
{
int[,] WorldMap = RaycastGame.Instance.WorldMap;
try
{
if (WorldMap[(int)(mapPos.X + Size), (int)(mapPos.Y + Size)] > 0 ||
WorldMap[(int)(mapPos.X - Size), (int)(mapPos.Y - Size)] > 0 ||
WorldMap[(int)(mapPos.X + Size), (int)(mapPos.Y - Size)] > 0 ||
WorldMap[(int)(mapPos.X - Size), (int)(mapPos.Y + Size)] > 0)
{
return true;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return false;
}
private bool CheckSpriteCollision(Vector2 mapPos)
{
foreach (Object sprite in RaycastGame.Instance.SpriteList)
{
if (!sprite.Block) continue;
if ((sprite.PosX == (int)(mapPos.X + Size) && sprite.PosY == (int)(mapPos.Y + Size)) ||
(sprite.PosX == (int)(mapPos.X - Size) && sprite.PosY == (int)(mapPos.Y - Size)) ||
(sprite.PosX == (int)(mapPos.X + Size) && sprite.PosY == (int)(mapPos.Y - Size)) ||
(sprite.PosX == (int)(mapPos.X - Size) && sprite.PosY == (int)(mapPos.Y + Size)))
{
return true;
}
}
return false;
}
}
}