-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCannon.cs
More file actions
89 lines (81 loc) · 2.71 KB
/
Cannon.cs
File metadata and controls
89 lines (81 loc) · 2.71 KB
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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgGame
{
public class Cannon
{
private GameLevel level;
private float moveSpeed = 3;
private Vector2 position;
public Vector2 projectilePosition;
private Texture2D texture;
private Texture2D projectileTexture;
private Direction direction;
private bool projectileAlive = false;
public Rectangle projectileLocalBounds;
public Rectangle ProjectileBoundingRectangle
{
get
{
int left = (int)Math.Round(projectilePosition.X - (projectileTexture.Width / 2)) + projectileLocalBounds.X;
int top = (int)Math.Round(projectilePosition.Y - projectileTexture.Height) + projectileLocalBounds.Y - 5;
return new Rectangle(left, top, projectileLocalBounds.Width, projectileTexture.Height / 2);
}
}
public Cannon(GameLevel level, Direction direction, Vector2 position)
{
this.direction = direction;
this.level = level;
this.position = position;
texture = level.content.Load<Texture2D>("tiles/cannon");
projectileTexture = level.content.Load<Texture2D>("tiles/cannon_projectile");
int width = (int)(projectileTexture.Width * 0.6f);
int left = (projectileTexture.Width - width) / 2;
int height = (int)(projectileTexture.Width * 0.6f);
int top = projectileTexture.Height - height;
projectileLocalBounds = new Rectangle(left, top, width, height);
SpawnNewProjectile();
}
public void Update()
{
if (projectileAlive)
{
projectilePosition = MoveProjectile();
if (level.CheckForSolidCollision(ProjectileBoundingRectangle))
projectileAlive = false;
return;
}
SpawnNewProjectile();
}
public void SpawnNewProjectile()
{
projectilePosition = position;
projectileAlive = true;
}
public Vector2 MoveProjectile()
{
switch (direction)
{
case Direction.Up:
return new Vector2(projectilePosition.X, projectilePosition.Y - moveSpeed);
case Direction.Down:
return new Vector2(projectilePosition.X, projectilePosition.Y + moveSpeed);
case Direction.Left:
return new Vector2(projectilePosition.X - moveSpeed, projectilePosition.Y);
case Direction.Right:
return new Vector2(projectilePosition.X + moveSpeed, projectilePosition.Y);
}
return projectilePosition;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(projectileTexture, projectilePosition, null, Color.White, 0, new Vector2(texture.Height / 2, texture.Width), 1.0f, SpriteEffects.None, 0.0f);
spriteBatch.Draw(texture, position, null, Color.White, 0, new Vector2(texture.Height / 2, texture.Width), 1.0f, SpriteEffects.None, 0.0f);
}
}
}