Skip to content

Commit

Permalink
Fix hit detection bug for player
Browse files Browse the repository at this point in the history
`Player` wasn't having hit detection because of the mirrored `scaleX`.

It might be a bug in Futile, so I just modified the
`GetTextureRectRelativeToContainer()` method in order to "fix" it. An
issue has been filed
[here](MattRix/Futile#215).
  • Loading branch information
wongjustin99 committed Nov 20, 2013
1 parent 7892703 commit dc75e4e
Show file tree
Hide file tree
Showing 28 changed files with 642 additions and 273 deletions.
5 changes: 0 additions & 5 deletions Assets/FutileDemos.meta

This file was deleted.

40 changes: 40 additions & 0 deletions Assets/Scripts/BasicEnemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class BasicEnemy : Enemy
{
private float speedX = 3.0f;
private float speedY = 0.0f;
private float shotrate = RXRandom.Range (60.0f,80.0f);

private float frameCount = 0;

public BasicEnemy() : base("Monkey_0")
{
this.x = Futile.screen.halfWidth;
this.y = RXRandom.Range(-Futile.screen.halfHeight, Futile.screen.halfHeight);
// once the sprites are swopped out, this needs to be forgone for
// sprites with proper resolution in the first place
this.scale = 0.25f;
}

public BasicEnemy( float x, float y ) : base(x, y)
{
}

override public void HandleUpdate()
{

this.x -= speedX;
this.y += speedY;

// enemy shoots
if(frameCount % shotrate == 0)
{
_shotStrategy.shoot(this.x, this.y, true);
}
frameCount += 1;
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/BasicEnemy.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/Scripts/BasicShotStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
public class BasicShotStrategy : ShotStrategy {
public void shoot( float x, float y, bool isEnemy )
{
Shot shotToCreate = new Shot(x, y, isEnemy);
if (isEnemy)
ShotManager.addEnemyShot(shotToCreate);
else
ShotManager.addPlayerShot(shotToCreate);
Shot shotToCreate = new Shot(x, y, isEnemy);
if (isEnemy)
ShotManager.addEnemyShot(shotToCreate);
else
ShotManager.addPlayerShot(shotToCreate);
}
}
38 changes: 38 additions & 0 deletions Assets/Scripts/BossEnemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System;

public class BossEnemy : Enemy
{
private float speedX = 0.5f;
private float shotrate = 40.0f;
private float frameCount = 0;
private int direction = -1;

public BossEnemy() : base("Monkey_0")
{
this.x = Futile.screen.halfWidth;
this.y = 0f;
this.scale = 0.5f;
}

public BossEnemy( float x, float y) : base(x, y)
{
}

override public void HandleUpdate()
{
//movement of boss, slowly moves across screen and up and down
this.x -= speedX;
this.y -= direction * 1.5f;
if (this.y >= Futile.screen.halfHeight - 10f || this.y <= -Futile.screen.halfHeight + 10f)
direction = direction * -1;

// enemy shoots
if (frameCount % shotrate == 0)
{
_shotStrategy.shoot(this.x, this.y, true);
}
frameCount += 1;
}

}
8 changes: 8 additions & 0 deletions Assets/Scripts/BossEnemy.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions Assets/Scripts/Controls/ControlScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ public abstract class ControlScheme : FContainer {
// target is what the ControlScheme will move
protected Player _target;

public ControlScheme( Player target ){
public ControlScheme(){
}

public ControlScheme( Player target ){
setTarget(target);
}

public void setTarget( Player target ){
this._target = target;
}

//default shooting behavior
public void HandleShoot()
{
_target.shoot();
if( _target != null )
_target.shoot();
}

public abstract void acceptTouchOne(FTouch touch);
Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/Controls/PadControlScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ public class PadControlScheme : ControlScheme {
private int speedX = 0;
private int speedY = 0;

public PadControlScheme( Player target ) : base( target ){
_target = target;
public PadControlScheme(){
pad = new GamePad();
AddChild (pad);
ListenForUpdate(HandleUpdate);
}

public void HandleUpdate()
{
_target.x += speedX;
_target.y += speedY;
if( _target != null ) {
_target.x += speedX;
_target.y += speedY;
}
}

override public void acceptTouchOne(FTouch touch)
Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/Controls/TouchControlScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
public class TouchControlScheme : ControlScheme
{

public TouchControlScheme( Player target ) : base( target ){
_target = target;
public TouchControlScheme(){
}

public void MoveCharacter(FTouch touch)
Expand Down
11 changes: 6 additions & 5 deletions Assets/Scripts/Controls/XboxControlScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ public class XboxControlScheme : ControlScheme {
private float speedX = 0;
private float speedY = 0;

public XboxControlScheme( Player target ) : base( target ){
_target = target;
public XboxControlScheme(){
ListenForUpdate(HandleUpdate);
}

public void HandleUpdate()
{
_target.x += speedX;
_target.y += speedY;
decodeTouch();
if( _target != null ){
_target.x += speedX;
_target.y += speedY;
decodeTouch();
}
}

// these touch mehods aren't used
Expand Down
60 changes: 28 additions & 32 deletions Assets/Scripts/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@
using System.Collections.Generic;
using System;

public class Enemy : FSprite
public abstract class Enemy : FSprite
{
private float speedX = 3.0f;
private float speedY = 0.0f;
private float shotrate = RXRandom.Range (60.0f,80.0f);
private float frameCount = 0;
protected ShotStrategy _shotStrategy;

ShotStrategy _shotStrategy;

public Enemy() : base("fish-fred")
{
this.x = Futile.screen.halfWidth;
this.y = RXRandom.Range(-Futile.screen.halfHeight, Futile.screen.halfHeight);
this.scale = 0.25f;
ListenForUpdate (HandleUpdate);
}
public Enemy() : base("fish-fred")
{
// default shotStrategy
_shotStrategy = new BasicShotStrategy();
this.x = Futile.screen.halfWidth;
this.y = RXRandom.Range(-Futile.screen.halfHeight, Futile.screen.halfHeight);
// once the sprites are swopped out, this needs to be forgone for
// sprites with proper resolution in the first place
this.scale = 0.25f;
ListenForUpdate (HandleUpdate);
}

public void setShotStrategy( ShotStrategy _shotStrategy )
{
this._shotStrategy = _shotStrategy;
}

public void HandleUpdate()
{

this.x -= speedX;
this.y += speedY;
public Enemy( float x, float y ) : this()
{
this.x = x;
this.y = y;
}

// enemy shoots
if(frameCount%shotrate == 0)
{
_shotStrategy.shoot(this.x, this.y, true);
}
frameCount += 1;
}
}
public Enemy( string sprite ) : base(sprite)
{
}

public void setShotStrategy( ShotStrategy _shotStrategy )
{
this._shotStrategy = _shotStrategy;
}
public abstract void HandleUpdate();
}
30 changes: 30 additions & 0 deletions Assets/Scripts/EnemyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public static class EnemyFactory
{
public static Enemy generateEnemy( string enemyName, float x, float y )
{
// this should really be changed, so LevelEvents have some
// sort of EnemyType built into them, so they can be type-
// checked versus being a string.
//

if( enemyName.Equals( "enemy1" ) )
{
return new BasicEnemy(x, y);
} else if( enemyName.Equals( "boss1" ) )
{
Enemy _myEnemy = new BossEnemy(x, y);
_myEnemy.setShotStrategy(new FanShotStrategy());
return _myEnemy;
} else
{
Debug.Log( "not a valid EnemyType!" );
return null;
}

}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/EnemyFactory.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions Assets/Scripts/FanShotStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using System.Collections;

public class FanShotStrategy : ShotStrategy {
public void shoot( float x, float y, bool isEnemy )
{
Shot shotToCreate;
int[] angles = { 45, 15, 0, 315, 345};
public void shoot( float x, float y, bool isEnemy )
{
Shot shotToCreate;
int[] angles = { 45, 15, 0, 315, 345};

for (int i = 0; i < angles.Length; ++i)
{
float angle = angles[i]*Mathf.Deg2Rad;
shotToCreate = new Shot(x, y, isEnemy, angle) ;
if (isEnemy)
ShotManager.addEnemyShot(shotToCreate);
else
ShotManager.addPlayerShot(shotToCreate);
}
for (int i = 0; i < angles.Length; ++i)
{
float angle = angles[i]*Mathf.Deg2Rad;
shotToCreate = new Shot(x, y, isEnemy, angle);
if (isEnemy)
ShotManager.addEnemyShot(shotToCreate);
else
ShotManager.addPlayerShot(shotToCreate);
}
}
}
}
Loading

0 comments on commit dc75e4e

Please sign in to comment.