Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Assets/Creature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Creature
{
public Vector2 position;
public int age;
public GameObject go;

DNA dna;
float hunger;



public Creature(GameObject g, float x, float y)
{
//Random DNA
go = GameObject.Instantiate(g);
SetPosition(new Vector2(x, y));
dna = new DNA();
go.GetComponent<SpriteRenderer>().color = dna.randomColor;
go.GetComponent<Transform>().localScale = go.GetComponent<Transform>().localScale * dna.size;
age = 0;
}

public Creature(GameObject g, float x, float y, Creature parent1, Creature parent2)
{
//DNA constructor with mother and father DNA
go = GameObject.Instantiate(g);
SetPosition(new Vector2(x, y));
dna = new DNA(parent1.dna, parent2.dna);
go.GetComponent<SpriteRenderer>().color = dna.randomColor;
go.GetComponent<Transform>().localScale = go.GetComponent<Transform>().localScale * dna.size;
age = 0;
}

void SetPosition(Vector2 p)
{
position = p;
go.transform.position = p;
}

void Move(Vector2 movement)
{
SetPosition(position + movement);
}

public void Step()
{
Vector2 movement = new Vector2(Random.Range(0.05f, -0.05f), Random.Range(0.05f, -0.05f)) * dna.speed;
Move(movement);
}
}
11 changes: 11 additions & 0 deletions Assets/Creature.cs.meta

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

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

public class CreatureManager : MonoBehaviour
{

public static CreatureManager Instance;
public GameObject prefab;
public int initCreatures;

List<Creature> creatures;

private void Awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(this);
}
}

private void Start()
{
creatures = new List<Creature>();
for (int i = 0; i < initCreatures; i++)
{
creatures.Add(new Creature(prefab, Random.Range(-1,1),Random.Range(-1,1)));

}
}

private void Update()
{
for (int i = 0; i < creatures.Count; i++)
{
creatures[i].Step();
creatures[i].age++;
if(creatures[i].age > 2000 || creatures.Count > 75)
{
print(" Death " + i);

Destroy(creatures[i].go);
creatures.RemoveAt(i);
}
for (int ii = 0; ii < creatures.Count; ii++)
{
if (i != ii && creatures[i].age > 500 && creatures[ii].age > 500)
{
if (Vector2.Distance(creatures[i].position, creatures[ii].position) < 0.075)
{
print("Birth");
Vector2 placeOfBirth = new Vector2(creatures[i].position.x, creatures[i].position.y);

creatures.Add(new Creature(prefab, placeOfBirth.x, placeOfBirth.y, creatures[i], creatures[ii]));
}
}
}
}
}

public void RegisterCreature(Transform transform)
{
creatures.Add(new Creature(prefab, transform.position.x, transform.position.y));
}
}
11 changes: 11 additions & 0 deletions Assets/CreatureManager.cs.meta

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

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

public class DNA
{
float colorR = Random.value, colorG = Random.value, colorB = Random.value, colorA = 255f;
float curiosity, aggression, hungerSpeed;

public float size, speed;
public Color randomColor;

public DNA()
{
//Random DNA
randomColor = new Color(colorR, colorG, colorB, colorA);
size = Random.Range(1f, 4f);
speed = Random.Range(1f, 3f);
curiosity = Random.Range(1f, 10f);
aggression = Random.Range(1f, 10f);
hungerSpeed = Random.Range(1f, 5f);
}

public DNA(DNA father, DNA mother)
{
//Randomly chooses values from mother and father.
randomColor = mateColor(father.randomColor, mother.randomColor);
size = mateValue(father.size, mother.size);
speed = mateValue(father.size, mother.size);
curiosity = mateValue(father.size, mother.size);
aggression = mateValue(father.aggression, mother.aggression);
hungerSpeed = mateValue(father.hungerSpeed, mother.hungerSpeed);

}

public DNA(DNA father, DNA mother, float mutationProbability)
{
//Same as the one before but randomly changes some values.
randomColor = mateColor(father.randomColor, mother.randomColor);
size = mateValue(father.size, mother.size);
speed = mateValue(father.size, mother.size);
curiosity = mateValue(father.size, mother.size);
aggression = mateValue(father.aggression, mother.aggression);
hungerSpeed = mateValue(father.hungerSpeed, mother.hungerSpeed);
}

Color mateColor(Color val1, Color val2)
{
float r = Random.value;

if (r > 0.5)
{
return val1;
}
else
{
return val2;
}
}


float mateValue(float val1, float val2)
{
float r = Random.value;

if(r > 0.5)
{
return val1;
}
else
{
return val2;
}
}
}
11 changes: 11 additions & 0 deletions Assets/DNA.cs.meta

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

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

public class DNADescription : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
CreatureManager.Instance.RegisterCreature(transform);
}

// Update is called once per frame
void Update()
{

}
}
11 changes: 11 additions & 0 deletions Assets/DNADescription.cs.meta

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

81 changes: 81 additions & 0 deletions Assets/New Sprite.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &9152431475589012337
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8827183501107575473}
- component: {fileID: 6262412542377855864}
m_Layer: 0
m_Name: New Sprite
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8827183501107575473
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9152431475589012337}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!212 &6262412542377855864
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9152431475589012337}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 10913, guid: 0000000000000000f000000000000000, type: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 0.16, y: 0.16}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
7 changes: 7 additions & 0 deletions Assets/New Sprite.prefab.meta

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

8 changes: 8 additions & 0 deletions Assets/Scenes.meta

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

Loading