Working version to begin neural net stuff on
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 10 Mar 2019 01:09:10 +0000 (19:09 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 10 Mar 2019 01:09:10 +0000 (19:09 -0600)
conf.lua
main.lua
src/utils.lua
src/world.lua

index d29aeaac06ac499e0b666b1fff13464d4e1bf04c..26980e84dfc2341fc2566a9acd283d84208dbad1 100644 (file)
--- a/conf.lua
+++ b/conf.lua
@@ -1,9 +1,12 @@
+-- Window will be small since the graphics are not very important
+local WINDOW_WIDTH = 800
+local WINDOW_HEIGHT = 600
+
 function love.conf(t)
        t.window.title = "Maching Learning Game"
 
-       -- Window will be small since the graphics are not very important
-       t.window.width = 640
-       t.window.height = 480
+       t.window.width = WINDOW_WIDTH
+       t.window.height = WINDOW_HEIGHT
 
        t.window.vsync = true
 
@@ -44,6 +47,8 @@ local ENEMY_COLOR = { 1, 0, 0 }
 local BULLET_COLOR = { 0.6, 0.6, 1.0 }
 
 return {
+       WINDOW_WIDTH = WINDOW_WIDTH;
+       WINDOW_HEIGHT = WINDOW_HEIGHT;
        KEYS = KEYMAP;
 
        BACK_COLOR = BACK_COLOR;
index 156915decbb8b0c2dbe05c27885fc38dc3d3fb21..eb33d6814bd498d2e36fd5473661cd6d18b294c5 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -4,11 +4,16 @@ local world_mod = require "src.world"
 local Input = require "src.input"
 
 local World = world_mod.World
+local Enemy = world_mod.Enemy
 
 local world, player
 local input
 function love.load()
        world, player = World:new()
+       for i = 1, 10 do
+               local enemy = Enemy:new(math.random(800), math.random(600))
+               world:add_entity(enemy)
+       end
 
        input = Input:new()
 
index d98d3042ce2abf70a9f340bd50c6b5435d90ea43..6a20ac0ace7764cd62674201273abd9ca5677f11 100644 (file)
@@ -13,6 +13,10 @@ function math.genuuid()
     end)
 end
 
+function math.rectintersects(r1, r2)
+       return r1[1] <= r2[1] + r2[3] and r1[2] <= r2[2] + r2[4] and r1[1] + r1[3] >= r2[1] and r1[2] + r1[4] >= r2[2]
+end
+
 local function ripairsiter(t, i)
        i = i - 1
        if i ~= 0 then
index 811dbe55fe9583e52779c12746ae18d217985dd0..01906453d5327e85d51941e08d0bd508de4aaa19 100644 (file)
@@ -6,13 +6,15 @@ local CONF = require "conf"
 
 local Bullet = {}
 local Bullet_mt = { __index = Bullet }
+Bullet.ENTITY_TYPE = "Bullet"
+
 function Bullet:new(x, y, vx, vy)
        local o = {
                x = x;
                y = y;
                vx = vx;
                vy = vy;
-               life = 100;
+               life = 80;
                alive = true;
        }
        
@@ -20,16 +22,27 @@ function Bullet:new(x, y, vx, vy)
        return o
 end
 
-function Bullet:update(dt)
-       self.x = self.x + self.vx * dt
-       self.y = self.y + self.vy * dt
+function Bullet:update(dt, world)
+       world:move_entity(self, self.vx * dt, self.vy * dt)
 
        self.life = self.life - 1
        if self.life <= 0 then
-               self.alive = false
+               world:remove_entity(self)
+       end
+end
+
+function Bullet:collide(other, dx, dy, world)
+       if other.ENTITY_TYPE == "Enemy" then
+               world:remove_entity(other)
+               world:remove_entity(self)
        end
 end
 
+function Bullet:get_rect()
+       local R = 8 * .7
+       return { self.x - R, self.y - R, R * 2, R * 2 }
+end
+
 function Bullet:draw()
        local R = 8
        local cx = self.x
@@ -62,11 +75,13 @@ end
 
 local Player = {}
 local Player_mt = { __index = Player }
+Player.ENTITY_TYPE = "Player"
+
 function Player:new()
        local o = {
-               x = 0;
-               y = 0;
-               r = 16;
+               x = CONF.WINDOW_WIDTH / 2;
+               y = CONF.WINDOW_HEIGHT / 2;
+               r = 20;
                fire_cooldown = 0;
        }
        
@@ -85,11 +100,9 @@ function Player:update(dt, world, input)
        if input.move_left  then dx = dx - SPEED end
        if input.move_right then dx = dx + SPEED end
 
-       self.x = self.x + dx * dt
-       self.y = self.y + dy * dt
+       world:move_entity(self, dx * dt, dy * dt)
 
        if self.fire_cooldown <= 0 then
-               self.fire_cooldown = 10
                local firex = 0
                local firey = 0
 
@@ -101,6 +114,8 @@ function Player:update(dt, world, input)
                if input.fire_right then firex = firex + 1 end
 
                if firex ~= 0 or firey ~= 0 then
+                       self.fire_cooldown = 6
+
                        local d = math.sqrt(math.sqrDist(0, 0, firex, firey))
                        firex = FIRE_SPEED * firex / d
                        firey = FIRE_SPEED * firey / d
@@ -117,11 +132,62 @@ function Player:fire(vx, vy, world)
        world:add_entity(bullet)        
 end
 
+function Player:get_rect()
+       return { self.x - self.r, self.y - self.r, self.r * 2, self.r * 2 }
+end
+
+function Player:collide(other, dx, dy, world)
+end
+
 function Player:draw()
        love.graphics.setColor(CONF.PLAYER_COLOR)
        love.graphics.circle("fill", self.x, self.y, self.r)
 end
 
+-- ENEMY --
+
+local Enemy = {}
+local Enemy_mt = { __index = Enemy }
+Enemy.ENTITY_TYPE = "Enemy"
+
+function Enemy:new(x, y)
+       local o = {
+               x = x;
+               y = y;
+               size = 20;
+       }
+
+       setmetatable(o, Enemy_mt)
+       return o
+end
+
+function Enemy:update(dt, world)
+       local player = world.player
+       
+       local a = math.atan2(player.y - self.y, player.x - self.x)
+       local dx = math.cos(a)
+       local dy = math.sin(a)
+
+       local SPEED = 80
+       world:move_entity(self, dx * dt * SPEED, dy * dt * SPEED)
+end
+
+function Enemy:get_rect()
+       return { self.x - self.size, self.y - self.size, self.size * 2, self.size * 2 }
+end
+
+function Enemy:collide(other, dx, dy, world)
+       if other.ENTITY_TYPE == "Enemy" then
+               self.x = self.x - dx
+               self.y = self.y - dy
+       end
+end
+
+function Enemy:draw()
+       love.graphics.setColor(CONF.ENEMY_COLOR)
+       love.graphics.rectangle("fill", unpack(self:get_rect()))
+end
+
 -- WORLD --
 
 local World = {}
@@ -182,6 +248,27 @@ function World:remove_entity(ent_or_id)
        table.remove(self.entities, pos)
 end
 
+-- Assumes ent has x and y
+function World:move_entity(ent, dx, dy)
+       ent.x = ent.x + dx
+       for _, e in ipairs(self.entities) do
+               if e.id ~= ent.id then
+                       if math.rectintersects(e:get_rect(), ent:get_rect()) then
+                               ent:collide(e, dx, 0, self)
+                       end
+               end
+       end
+
+       ent.y = ent.y + dy
+       for _, e in ipairs(self.entities) do
+               if e.id ~= ent.id then
+                       if math.rectintersects(e:get_rect(), ent:get_rect()) then
+                               ent:collide(e, 0, dy, self)
+                       end
+               end
+       end
+end
+
 function World:draw()
        for _, e in ipairs(self.entities) do
                e:draw()
@@ -193,5 +280,6 @@ end
 return {
        World = World;
        Player = Player;
+       Enemy = Enemy;
        Bullet = Bullet;
 }