[Help] making enemies

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

[Help] making enemies

Post by pactace »

I am making a 2D plaformer and I dont really know how to make an enemy and import sprites and such can anyone help me
Last edited by pactace on Wed Feb 04, 2015 12:10 am, edited 3 times in total.
Very new programmer dont judge
User avatar
Bhane
Prole
Posts: 6
Joined: Sun Feb 01, 2015 7:11 pm

Re: Im a noob somebody help me

Post by Bhane »

Are you working from another project?
Post your .love so we can see some context.
User avatar
Duster
Prole
Posts: 32
Joined: Fri Dec 19, 2014 8:34 pm
Location: Ontario, Canada

Re: Im a noob somebody help me

Post by Duster »

Read the wiki, check tutorials on youtube, etc.
Love is well documented so if you don't know how to use something, chances are you can find the answer on the wiki
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Im a noob somebody help me

Post by pactace »

Code: Select all

local AdvTiledLoader = require( "AdvTiledLoader.Loader" )
require("camera")

function love.load()
	love.graphics.setBackgroundColor( 9, 166, 193 )
	AdvTiledLoader.path = "maps/"
	map = AdvTiledLoader.load("Ians maps.tmx")
	map:setDrawRange(0, 0, map.width * map.tileWidth, map.height * map.tileHeight)
	
	camera:setBounds(0, 0, map.width * map.tileWidth - love.graphics.getWidth(), map.height * map.tileHeight - love.graphics.getHeight() )

	world = 	{
				gravity = 1536,
				ground = 512,
				}
				
	player = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -1024,
				speed = 512,
				flySpeed = 700,
				state = "",
				h = 32,
				w = 32,
				standing = false,
				}
	function player:jump()
		if self.standing then
			self.y_vel = self.jump_vel
			self.standing = false
		end
	end
	
	function player:right()
		self.x_vel = self.speed
	end
	
	function player:left()
		self.x_vel = -1 * (self.speed)
	end
	
	function player:stop()
		self.x_vel = 0
	end
	
	function player:collide(event)
		if event == "floor" then
			self.y_vel = 0
			self.standing = true
		end
		if event == "cieling" then
			self.y_vel = 0
		end
	end
	
	function player:update(dt)
		local halfX = self.w / 2
		local halfY = self.h / 2
		
		self.y_vel = self.y_vel + (world.gravity * dt)
		
		self.x_vel = math.clamp(self.x_vel, -self.speed, self.speed)
		self.y_vel = math.clamp(self.y_vel, -self.flySpeed, self.flySpeed)
		
		local nextY = self.y + (self.y_vel*dt)
		if self.y_vel < 0 then
			if not (self:isColliding(map, self.x - halfX, nextY - halfY))
				and not (self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
				self.y = nextY
				self.standing = false
			else
				self.y = nextY + map.tileHeight - ((nextY - halfY) % map.tileHeight)
				self:collide("cieling")
			end
		end
		if self.y_vel > 0 then
			if not (self:isColliding(map, self.x-halfX, nextY + halfY))
				and not(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
					self.y = nextY
					self.standing = false
			else
				self.y = nextY - ((nextY + halfY) % map.tileHeight)
				self:collide("floor")
			end
		end
		
		local nextX = self.x + (self.x_vel * dt)
		if self.x_vel > 0 then
			if not(self:isColliding(map, nextX + halfX, self.y - halfY))
				and not(self:isColliding(map, nextX + halfX, self.y + halfY - 1)) then
				self.x = nextX
			else
				self.x = nextX - ((nextX + halfX) % map.tileWidth)
			end
		elseif self.x_vel < 0 then
			if not(self:isColliding(map, nextX - halfX, self.y - halfY))
				and not(self:isColliding(map, nextX - halfX, self.y + halfY - 1)) then
				self.x = nextX
			else
				self.x = nextX + map.tileWidth - ((nextX - halfX) % map.tileWidth)
			end
		end
		
		self.state = self:getState()
	end
	
	function player:isColliding(map, x, y)
		local layer = map.tl["Solid"]
		local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
		local tile = layer.tileData(tileX, tileY)
		return not(tile == nil)
	end
	
	function player:getState()
		local tempState = ""
		if self.standing then
			if self.x_vel > 0 then
				tempState = "right"
			elseif self.x_vel < 0 then
				tempState = "left"
			else
				tampState = "stand"
			end
		end
		if self.y_vel > 0 then
			tempState = "fall"
		elseif self.y_vel < 0 then
			tempState = "jump"
		end
		return tempState
	end
	xCloud = 0
	
end

function love.draw()
	camera:set()
	
	
	love.graphics.setColor( 0, 0, 0 )
	love.graphics.rectangle( "line", player.x - player.w/2, player.y - player.h/2, player.w, player.h)
	
	love.graphics.setColor( 255, 255, 255 )
	love.graphics.rectangle( "fill", xCloud - 256, 126, 256, 64 )
	
	map:draw()
		
camera:unset()
end

function love.update(dt)
	xCloud = xCloud + 32*dt
	if xCloud >= (800 + 25 ) then
		xCloud = 0
	end

	if dt > 0.05 then
		dt = 0.05
	end
	if love.keyboard.isDown("d") then
		player:right()
	end
	if love.keyboard.isDown("a") then
		player:left()
	end
	if love.keyboard.isDown(" ") then
		player:jump()
	end
	
	player:update(dt)
	
	camera:setPosition( player.x - (love.graphics.getWidth()/2), player.y - (love.graphics.getHeight()/2))
end

function love.keyreleased(key)
	if (key == "a") or (key == "d") then
		player.x_vel = 0
	end
	
end
Last edited by pactace on Tue Feb 03, 2015 11:56 pm, edited 3 times in total.
Very new programmer dont judge
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Im a noob somebody help me

Post by pactace »

sorry for the long code didnt know what to post]
Very new programmer dont judge
User avatar
Bhane
Prole
Posts: 6
Joined: Sun Feb 01, 2015 7:11 pm

Re: Im a noob somebody help me

Post by Bhane »

So can you be more precise about what you want?
ex, What kind of enemy? What behaviours would you like it to have? How do you want to use sprites?
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Im a noob somebody help me

Post by Jasoco »

pactace wrote:sorry for the long code didnt know what to post]
Can you please use the CODE tags?
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Im a noob somebody help me

Post by Doctory »

for the love of god use descriptive titles
"im a noob somebody help me" does not help anyone
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Im a noob somebody help me

Post by pactace »

Sorry about the name I'm just looking how to make an enemy that attacks you when you get close I already got all the sprites
Very new programmer dont judge
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Im a noob somebody help me

Post by pactace »

Also I'm unfamiliar with code tags
Very new programmer dont judge
Post Reply

Who is online

Users browsing this forum: DarkblooM and 4 guests