Commit c884d330 authored by Luke A Smith's avatar Luke A Smith

Merge branch 'flameCrab' into 'master'

Flame crab

flameCrab and heap for pathfinding

See merge request !5
parents ddcf4c90 c7583795
fileFormatVersion: 2
guid: 3449ac6e2a03fd54fa5d10fa4b3518ac
timeCreated: 1475798950
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class FlameCrab : MonoBehaviour {
//update once grid is ready
//array of positions to move to once grid is ready
//Vector3 [] positions;
public float moveDelay = 5f;
public float speed = 5f;
public float health = 10f;
public int moveDistance = 5;
float nextMove = 0f;
Vector3 targetLocation = -Vector3.up;
Rigidbody2D body;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if (nextMove < Time.time)
{
if (targetLocation != -Vector3.up)
{
if ((transform.position - targetLocation).sqrMagnitude > .5f)
{
Move();
}
else
{
nextMove = Time.time + moveDelay;
targetLocation = -Vector3.up;
}
}
else
{
//find new target location to right or left of enemy
int spacesToMove = Random.Range(-moveDistance, moveDistance+1);
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, spacesToMove, 1);
if (hit.collider)
{
targetLocation = hit.point;
}
else
{
targetLocation = transform.position + (transform.right * spacesToMove);
}
}
}
}
void Move()
{
Vector3 dir = targetLocation - transform.position;
body.transform.Translate(dir.normalized * speed * Time.deltaTime);
}
}
fileFormatVersion: 2
guid: ad04fbd2b7592974baa491ea766a0337
timeCreated: 1475799560
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Heap <Tile> where Tile : HeapItem<Tile>{
Tile[] heapTiles;
int itemCount;
//replace openList with Heap
//add heapIndex, compareTo to tile class
//compare fCost of node then hCost
//item.compareTo(item2) should return pos if item is less than item2, neg if item is greater than item2
//create heap
public Heap(int maxSize)
{
heapTiles = new Tile[maxSize];
}
//add item into heap and sort it into position
public void Add(Tile item)
{
item.heapIndex = itemCount;
heapTiles[itemCount] = item;
SortUp(item);
itemCount++;
}
//remove first tile and resort heap
public Tile RemoveFirst()
{
Tile first = heapTiles[0];
itemCount--;
heapTiles[0] = heapTiles[itemCount];
heapTiles[0].heapIndex = 0;
SortDown(heapTiles[0]);
return first;
}
//resort item position
public void UpdateItem(Tile item)
{
SortUp(item);
}
public int Count
{
get
{
return itemCount;
}
}
//does heap contain item
public bool Contains(Tile item)
{
return Equals(heapTiles[item.heapIndex], item);
}
//sort tile lower into heap
void SortDown(Tile item)
{
while (true)
{
int leftChildIndex = item.heapIndex * 2 + 1;
int rightChildIndex = (item.heapIndex * 2) + 2;
int swapIndex = 0;
if (leftChildIndex < itemCount)
{
swapIndex = leftChildIndex;
if (rightChildIndex < itemCount)
{
if (heapTiles[leftChildIndex].CompareTo(heapTiles[rightChildIndex]) < 0)
{
swapIndex = rightChildIndex;
}
}
if (item.CompareTo(heapTiles[swapIndex]) < 0)
{
Swap(item, heapTiles[swapIndex]);
}
else
{
return;
}
}
else
{
return;
}
}
}
//sort item higher into heap
void SortUp(Tile item)
{
int indexOfParent = (item.heapIndex - 1) / 2;
while (true)
{
Tile parent = heapTiles[indexOfParent];
if (item.CompareTo(parent) > 0)
{
Swap(item, parent);
}
else
{
break;
}
indexOfParent = (item.heapIndex - 1) / 2;
}
}
//swap tiles
void Swap(Tile item1, Tile item2)
{
heapTiles[item1.heapIndex] = item2;
heapTiles[item2.heapIndex] = item1;
int tempIndex = item1.heapIndex;
item1.heapIndex = item2.heapIndex;
item2.heapIndex = tempIndex;
}
}
public interface HeapItem<Tile> : IComparable<Tile>
{
int heapIndex
{
get;
set;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 243bb794b9f808b409bcd50557270e5b
timeCreated: 1477007304
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment