Skip to content

Commit

Permalink
Fixed weapon drop bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj committed Mar 17, 2022
1 parent 71de328 commit 7e8f54c
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 16 deletions.
Binary file added Assets/Knife.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/LancasterGuy_Knife_Attacked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/LancasterGuy_Knife_Resting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions CampusCrawl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="Characters\Player.cs" />
<Compile Include="Entities\Entity.cs" />
<Compile Include="Entities\Weapon.cs" />
<Compile Include="Entities\Weapons\Knife.cs" />
<Compile Include="Entities\Weapons\Fists.cs" />
<Compile Include="Game.cs" />
<Compile Include="Scenes\AlexandraSquare.cs" />
Expand Down
12 changes: 8 additions & 4 deletions Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Character : GameObject
{
protected bool followingPath = false;
protected BoxColliderComponent collider;
protected SpriteComponent sprite;
public SpriteComponent sprite;
protected float speed;
public float health;
public bool attacking;
Expand Down Expand Up @@ -124,8 +124,12 @@ public void CreateAndSetWeapon(Weapon weapon)

public void DropCurrentWeapon()
{
doNotPickUp = weapon;
weapon.PutDown();
if (weapon != null)
{
doNotPickUp = weapon;
weapon.PutDown();
weapon = null;
}
}

public float[] mouseDirection()
Expand Down Expand Up @@ -178,10 +182,10 @@ public void ScanAndPickUpEntities()
{
if (Scene.GridToTileLocation(entity.Position) == Scene.GridToTileLocation(this.Position) && entity != doNotPickUp)
{
//UNCOMMENT
// This means, we have an entity that we can pick up.
entity.PickedUp();
entity.Initialise(Scene, Layer);

if (entity is Weapon)
{
weapon = (Weapon)entity;
Expand Down
11 changes: 11 additions & 0 deletions Characters/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Player()
Scale = new Vector2(1, 1)
};
AddComponent(sprite);
TriggersEvents = true;
attackCooldown = new Timer(0.1f);
attackCooldown.OnTick += Cooldown;
attackCooldown.Loop = true;
Expand Down Expand Up @@ -280,6 +281,14 @@ private Vector2 calculateLookAt()
return positionNew;
}

public void ListenForDropWeapon()
{
if (InputHandler.HasEvent("DropWeapon"))
{
DropCurrentWeapon();
}
}

public override void Update(GameTime delta)
{
base.Update(delta);
Expand All @@ -298,6 +307,8 @@ public override void Update(GameTime delta)
angleA = (float)(Math.Atan(deltaPos.X / deltaPos.Y) + Math.PI);

sprite.Rotation = angleA;

ListenForDropWeapon();


if (health <= 0)
Expand Down
5 changes: 1 addition & 4 deletions Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace CampusCrawl.Entities
{
public class Entity : GameObject
{
private BoxColliderComponent collider;
private SpriteComponent sprite;
private string assetPath;

Expand Down Expand Up @@ -43,13 +42,11 @@ public void SetLocation(int x, int y)
public virtual void PickedUp()
{
RemoveComponent(sprite);
RemoveComponent(collider);
}

public virtual void PutDown()
{
AddComponent(sprite);
AddComponent(collider);
AddComponent(sprite);
}

public void Spawn(Vector2 playerPosition)
Expand Down
3 changes: 2 additions & 1 deletion Entities/Weapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public override void PutDown()
{
Texture = AssetManager.AttemptLoad<Texture2D>(playerModel),
Position = new Vector2(-16, -16),
Scale = new Vector2(1, 1)
Scale = new Vector2(1, 1),
Rotation = character.sprite.Rotation
});

Position = character.Position;
Expand Down
37 changes: 37 additions & 0 deletions Entities/Weapons/Knife.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using CampusCrawl.Characters;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tileEngine.SDK;
using tileEngine.SDK.Diagnostics;

namespace CampusCrawl.Entities.Weapons
{
internal class Knife : Weapon
{
int lungeSize = 10;
public Knife() : base("Assets/Knife.png", "Knife")
{
damage = 5;
range = 1;
knockback = 20;
}

public void Lunge(float multiplier,bool player)
{
range += 2;
damage += 1 * multiplier;
float[] attackDirection = character.mouseDirection();
if (!player)
attackDirection = character.attackDirection;
character.PushSelf(-(int)attackDirection[0] * lungeSize, -(int)attackDirection[1] * lungeSize);
Attack(true,player);
damage -= 1 * multiplier;
range -= 2;
}
}
}
1 change: 1 addition & 0 deletions Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void StartGame(Point location)
public override void Initialize()
{
TileEngine.Instance.MouseInput.AddBinding(MouseInputType.Position, "MousePosition");
TileEngine.Instance.KeyboardInput.AddBinding(Keys.F, "DropWeapon");
TileEngine.Instance.KeyboardInput.AddAxisBinding(Keys.D, Keys.A, Keys.S, Keys.W, "Movement");
UI.Initialize("Fonts/MakanHati-vmp94.ttf");
Texture2D logo = AssetManager.AttemptLoad<Texture2D>("logo.png");
Expand Down
23 changes: 16 additions & 7 deletions Scenes/AlexandraSquare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,24 @@ public override void Update(GameTime delta)
//...
}

[EventFunction("EnteredOrExitedShelteredArea")]
public void EnteredOrExitedShelteredArea(TileEventData e)
[EventFunction("OnShelteredArea")]
public void OnShelteredArea(TileEventData e)
{
DiagnosticsHook.DebugMessage("A");
Map.Layers.Where(x =>
TileLayer roofLayer = Map.Layers.Where(x => x.Name == "Roof").FirstOrDefault();
if (roofLayer != null)
{
DiagnosticsHook.DebugMessage(" -> " + x);
return true;
});
roofLayer.Opacity = (float)0.5;
}
}

[EventFunction("ExitedShelteredArea")]
public void ExitedShelteredArea(TileEventData e)
{
TileLayer roofLayer = Map.Layers.Where(x => x.Name == "Roof").FirstOrDefault();
if (roofLayer != null)
{
roofLayer.Opacity = 1;
}
}
}
}
Binary file modified campus-crawl.teproj
Binary file not shown.

0 comments on commit 7e8f54c

Please sign in to comment.